births <- openintro::ncbirthsLab 2: Working with Data Frames
Learning Objectives
Students will be able to:
- Increase skill and comfort with basic data management and syntax in R
- Create statistical graphics to answer questions about a data set
- Understand the basic methodology and syntax of the for loop
Readings
To prepare for this lab assignment read the following tutorials
Required Data and Packages
We will continue to work with the NC Births data used in Lab 1
For descriptions of the variables in this dataset, run the command ?openintro::ncbirths
Submit a single PDF for your lab report using Quarto
Section 1: Working with DataFrames
What are the names of the variables that indicate whether or not the mother smoked and the weight of the baby?
In addition to the variable that indicates whether or not the mother smoked, choose one numerical variable and one categorical variable that you think might also affect the weight of a baby. Explain why
Create a data frame that has only the variables
gender, premie, weight, andmature, and the observations for which the baby was not premature. Print out the first few rows using thehead()function.Create a data frame that has only the four variables you identified in
Question 1andQuestion 2and the observations for which the mother was a smoker. Print out the first few rows using thehead()functionReport the mean of
- the weights of all babies.
- the weights of those born to smoking mothers.
- the weights of those born to non-smoking mothers.
Hint: you are subsetting weights based on a condition
- Report the standard deviation of
- the weights of all babies
- the weights of those born to smoking mothers
- the weights of those born to non-smoking mothers
Section 2: Visualizing Data
Using
births, create two graphics that help answer this question: Does the birth weight of babies born to smoking mothers differ from those born to non-smoking mothers?Using
births, create two graphics that help answer this question: Does the number of prenatal medical visits differ between mothers who smoke and those who do not?
Section 3: For loops
A researcher measures the resting heart rates (beats per minute) of 10 participants:
heart_rates <- c(72, 85, 91, 68, 105, 77, 98, 115, 83, 95)Create a vector named
elevatedusingnumeric()with the same length asheart_ratesWrite a
forloop usingseq_along()to examine each participants heart rate. Withing the loop use the logical expressionheart_rates[i] > 100to determine if the participant has an elevated heart rate or not.Print the resulting
elevatedvectorInterpret the output of
mean(elevated)in the context of the problem