Use a function
The documentation on the mean() function gives us quite some information:
- The
mean()function computes the arithmetic mean. - The most general method takes multiple arguments:
xand.... - The
xargument should be a vector containing numeric, logical or time-related information.
Remember that R can match arguments both by position and by name. Can you still remember the difference? You'll find out in this exercise!
Once more, you'll be working with the view counts of your social network profiles for the past 7 days. These are stored in the linkedin and facebook vectors and have already been created for you.
This exercise is part of the course
Intermediate R
Exercise instructions
- Calculate the average number of views for both
linkedinandfacebookand assign the result toavg_liandavg_fb, respectively. Experiment with different types of argument matching! - Print out both
avg_liandavg_fb.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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 average number of views
# Inspect avg_li and avg_fb