Introduction to R (Part 2)

We believe in you!

Learning Objectives

After completing this lab, you should be able to:

Prepare for the Lab

Lab Activity

Researchers at the Vancouver and Okanagan campuses of The University of British Columbia have conducted a survey of child sleep. The results of this survey are found in the file child-sleep-data.csv. The table below provides names and descriptions for each variable in the data set.

</tr> </table> Download [child-sleep-data.csv](/assets/documents/child-sleep-data.csv) and then complete the following steps: ### Import and Inspect the Data * Import [child-sleep-data.csv](/assets/documents/child-sleep-data.csv) into R; assign it a meaningful name. * Inspect the `data.frame` using `str()`, `head()`, and `View()`. ### Convert Categorical Variables to Factors * Use `as.factor()` and `factor()` to convert any categorical variables to factors with the appropriate levels and labels. ### Subsetting and Logical Expressions Use `[]` to subset the data frame and find the answers to following questions/complete the tasks described below. You will also need to use ``, `max()`, `min()`, `mean()`, and logical expressions. * What campus was the 48th participant recruited at? * Return the data for participants who are missing responses on the sleep variable. * What proportion of participants are missing data for the sleep variable? * On average, how many hours do the children in this sample sleep per day? * What is the average daily sleep of children in each of the three conditions? * What is the participant ID of the participant(s) who reported the most sleep? The least sleep? ### Participant Information Use `table()` to identify how many, and what proportion of participants belong to each level of the three categorical variables.
Name Description
id A randomly generated participant ID, unique to each participant.
age</age> The age of the child in the study.
campus The campus from which the participant was recruited ("Point Grey" or "Okanagan").
sleep_location Where does the child sleep? 1 = co-sleeps with parent(s); 2 = sleeps in own room; 3 = shares a room but does not cosleep.
sleep The parent's estimate of the average hours of sleep the child receives in a 24-hour period.