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
filter. We are also going to review two different classes: data frames and vectors.
Read in the
msleep_ggplot2.csvfile with the function
read.csvand use the function
classto 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: the
nrowfunction 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 the
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 for
unlistand 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 just
summarizeto get the answer.