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.csvfile with the functionread.csvand use the functionclassto determine what type of object is returned. -
Now use the
filterfunction to select only the primates. How many animals in the table are primates? Hint: thenrowfunction 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
selectfunction to extract the sleep (total) for the primates. What class is this object? Hint: use%>%to pipe the results of thefilterfunction 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
meanfunction requires a vector so, if we simply apply it to the output above, we get an error. Look at the help file forunlistand use it to compute the desired average. -
For the last exercise, we could also use the dplyr
summarizefunction. We have not introduced this function, but you can read the help file and repeat exercise 5, this time using justfilterandsummarizeto get the answer.