dplyr exercises
Exercises
For these exercises, we will use a new dataset related to mammalian sleep. This data is described here. Download the CSV file from this location:
We are going to read in this data, then test your knowledge of they key dplyr
functions select
and filter
. We are also going to review two different classes: data frames and vectors.
-
Read in the
msleep_ggplot2.csv
file with the functionread.csv
and use the functionclass
to determine what type of object is returned. -
Now use the
filter
function to select only the primates. How many animals in the table are primates? Hint: thenrow
function gives you the number of rows of a data frame or matrix. -
What is the class of the object you obtain after subsetting the table to only include primates?
-
Now use the
select
function to extract the sleep (total) for the primates. What class is this object? Hint: use%>%
to pipe the results of thefilter
function toselect
. -
Now we want to calculate the average amount of sleep for primates (the average of the numbers computed above). One challenge is that the
mean
function requires a vector so, if we simply apply it to the output above, we get an error. Look at the help file forunlist
and use it to compute the desired average. -
For the last exercise, we could also use the dplyr
summarize
function. We have not introduced this function, but you can read the help file and repeat exercise 5, this time using justfilter
andsummarize
to get the answer.