Write your own function
# This is the recipe for writing your won function:
my_fun <- function(arg1, arg2) {
body
}
# Create a function pow_two(): it takes one argument and returns that number squared (that number times itself).
pow_tow <- function(x) {
(x^2)
}
test <- pow_tow(2)
test
## [1] 4
# create a function sum_abs(), that takes two arguments and returns the sum of the absolute values of both arguments.
sum_abs <- function(x,y){
(abs(x) + abs(y))
}
sum_abs.test <- sum_abs(-4 , 5)
sum_abs.test
## [1] 9
# Define a function, hello(). It prints out "Hi there!" and returns TRUE. It has no arguments.
hello <- function() {
print("Hi there!")
return(TRUE)
}
hello()
## [1] "Hi there!"
## [1] TRUE
# You can define default argument values in your own R functions as well. You can use the following recipe to do so:
my_fun <- function(arg1, arg2 = val2) {
body
}
# 1. Add an optional argument, named print_info, that is TRUE by default.
# 2. Wrap an if construct around the print() function: this function should only be executed if print_info is TRUE.
pow_two <- function(x) {
y <- x ^ 2
print(paste(x, "to the power two equals", y))
return(y)
}
# Answer
pow_two <- function(x, print_info = TRUE) {
y <- x ^ 2
if (print_info) {
print(paste(x, "to the power two equals", y))
}
return(y)
}
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
# Write a function that interprets the number of profile views on a single day:
# *The function takes one argument, num_views.
# *If num_views is greater than 15, the function prints out "You're popular!" to the console and returns num_views.
# *Else, the function prints out "Try to be more visible!" and returns 0.
# *Finally, call the interpret() function twice: on the first value of the linkedin vector and on the second element of the facebook vector.
interpret <- function(num_views) {
if (num_views > 15) {
print("You're popular!")
return(num_views)
} else {
print("Try to be more visible!")
return(0)
}
}
interpret(linkedin[1])
## [1] "You're popular!"
## [1] 16
interpret(facebook[2])
## [1] "Try to be more visible!"
## [1] 0
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
# Complete the following function:
# *Make return_sum an optional argument, that is TRUE by default.
# *Inside the for loop, iterate over all views: on every iteration, add the result of interpret(v) to count.
# *If return_sum is TRUE, return count
# *Else, return NULL.
interpret_all <- function(views, return_sum) {
count <- 0
for (v in views) {
}
if (return_sum) {
} else {
}
}
# Answer
interpret_all <- function(views, return_sum = TRUE) {
count <- 0
for (v in views) {
count <- count + interpret(v)
}
if (return_sum) {
return(count)
} else {
return(NULL)
}
}
# What function you can use to see the currently attached packages?
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
# write an efficient code that would install the whole needed packages (data.table and rjason)
# install.packages(c("data.table", "rjson"))