Introduction to R: Exercises


5. Functions. The base R distribution comes with many functions, such as mean() and sd(), which compute the mean and standard deviation of a collection of numbers.

You are given a vector of stock returns named returns. Use the aforementioned functions to compute its mean and standard deviation.


returns <- c(1.11, 2.51, -1.01, 3.94, -5.44, -10.10) # Compute the mean of the returns vector. mean_return <- ___(returns) # Compute the standard deviation of the returns vector. std_return <- sd(___) # Print the mean and standard deviation. print(mean_return) ___(___) # Compute the mean of the returns vector. mean_return <- mean(returns) # Compute the standard deviation of the returns vector. sd_return <- sd(returns) # Print the mean and standard deviation. print(mean_return) print(sd_return) test_error() test_object("mean_return", incorrect_msg="Did you use the `mean()` function?") test_object("sd_return", incorrect_msg="Did you use `returns` as the argument to `sd()`?") test_output_contains("print(mean_return)", incorrect_msg="Did you remember to print `mean_return`?") test_output_contains("print(std_return)", incorrect_msg="Did you remember to print `std_return`?") success_msg("Excellent work! Notice that `mean()` and `sd()` are part of the base package for `R`.")
Did you apply the `mean()` and `sd()` functions?

Previous Exercise Next Exercise