To consult the documentation on the sample() function, for example, you can use one of following R commands:
help(sample)
?sample
If you execute these commands in the console of the DataCamp interface, you'll be redirected to www.rdocumentation.org.
A quick hack to see the arguments of the sample() function is the args() function. Try it out in the console:
args(sample)
Then make the trim=0.2 rather than 0.
# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
# Calculate the mean of the sum
avg_sum<-mean(facebook+linkedin)
# Calculate the trimmed mean of the sum
avg_sum_trimmed<-mean(facebook+linkedin,trim=0.2)
# Inspect both new variables
args(avg_sum)
args(avg_sum_trimmed)
avg_sum_trimmed
avg_sum
na.rm that specified whether or not to remove missing values from the input vector before calculating the standard deviation.
trim: if trim =0.2, cut 20%, and get the rest of data to calculate average.
# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, NA, 17, 14)
facebook <- c(17, NA, 5, 16, 8, 13, 14)
# Basic average of linkedin
mean(linkedin)
# Advanced average of linkedin
mean(linkedin,trim=0,na.rm=TRUE)
The first result is NA, the second one is 12.333333333
Use abs() on linkedin - facebook to get the absolute differences between the daily Linkedin and Facebook profile views. Place the call to abs() inside mean() to calculate the Mean Absolute Deviation. In the mean() call, make sure to specify na.rm to treat missing values correctly!
# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, NA, 17, 14)
facebook <- c(17, NA, 5, 16, 8, 13, 14)
# Calculate the mean absolute deviation
abs(linkedin-facebook)
mean(abs(linkedin-facebook),trim=0,na.rm=TRUE)
read.table()function:
read.table("file.dat", header = TRUE, row.names = 1)
we should use "-" to connect content in abs()
Write you own function:
# Create a function pow_two()
pow_two<-function(a){
a*a
}
pow_two
# Use the function
pow_two(12)
# Create a function sum_abs()
sum_abs<-function(a,b){
abs(a)+abs(b)
}
# Use the function
sum_abs(-2,3)
There are situations in which your function does not require an input. Let's say you want to write a function that gives us the random outcome of throwing a fair die:
# Call the function hello()
hello()
throw_die <- function() {
number <- sample(1:6, size = 1)
number
}
throw_die()
about optional arguments:
# Finish the pow_two() function
pow_two <- function(x, print_info = TRUE) {
y <- x ^ 2
if (print_info) {
print(paste(x, "to the power two equals", y))
}
return(y)
}
# Some calls of the pow_two() function
pow_two(5)
pow_two(5, FALSE)
pow_two(5, TRUE)
And then for the use of if in the function:
# The linkedin and facebook vectors have already been created for you
# Define the interpret function
interpret <- function(num_views) {
if (num_views > 15) {
print("You're popular!")
return(num_views)
} else {
print("Try to be more visible!" )
return(0)
}
}
# Call the interpret function twice
interpret(linkedin)
interpret(facebook[2])
in this exercise, we have to use return() "return==0"is fault.
Also, we should use interpret(facebook[2])
although I don't know the meaning of this sentence:)
Then this exercise I did not complete at first.The requirement:
Finish the template for the interpret_all() 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. Remember that interpret(v) returns v for popular days, and 0 otherwise. At the same time, interpret(v) will also do some printouts.
Finish the if construct:
If return_sum is TRUE, return count
!!!!notice: interpret(v), this function has already defined before the loop we need to finish, at first I just notice the popular day but ignore the former function has already wiped out the days!!!!!!!!!!!!!!!!
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
# The interpret() can be used inside interpret_all()
interpret <- function(num_views) {
if (num_views > 15) {
print("You're popular!")
return(num_views)
} else {
print("Try to be more visible!")
return(0)
}
}
# Define the interpret_all() function
# views: vector with data to interpret
# return_sum: return total number of views on popular days?
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)
}
}
# Call the interpret_all() function on both linkedin and facebook
interpret_all(linkedin)
interpret_all(facebook)