Lab 1: Introduction to R
Learning Objectives
Students will be able to:
- Demonstrate basic R skills
- Use basic statistical functions
- Visualize single numerical and categorical variables
Section 1: Basic R Skills
Vectors
Create a vector named
heightsthat contains the heights, in inches, of yourself and two students near you. Print the contents of this vector.Create a vector named
namesthat contains the names of these people. Print the contents of this vectorTry typing
cbind(heights, names). What did this command do ? What class is this new object ?
Hint: Try the class() function
Downloading Data
Download the data set births.csv and upload it into R Studio. Store the data frame as
birthsvariable nameDemonstrate that you successfully uploaded the data by printing out the output of
head(births)How many observations and variables does the data set
birthscontain ?
Loading R packages
install the
openintropackage (console only, not in Quarto R chunk). Verify the isntallation by printing the output offind.package("openintro")Type the command
library(openintro)to load up the package.install the
tinyplotpackage (console only, not in Quarto R chunk). Verify the isntallation by printing the output offind.package("tinyplot")Type the command
library(tinyplot)to load up the package.
Vector Operations
Extract the
weightvariable as a vector from the data framebirthsWhat units do you think the weights are in ?
Create a new vector names
weight_in_poundswhich are the weights of the babies in pounds. You can look up conversion factors onlineRun the command
weight_in_pounds[1:20]and print out the output of the first 20 weights.
Section 2: Summarize Data (one variable)
What is the mean weight of the babies in pounds ?
What percentage of the mothers in the sample smoke ?
According to the Centers for Disease Control, approximately 9% of adult Americans are smokers. How far off is the percentage you found in (2.) from the CDC’s report?
Section 3: Visualizing Data (one numerical variable)
Produce a dot plot of the weights in pounds
Produce three different histograms of the weights in pounds. Use 3 bins, 20 bins, and 100 bins. Which histogram seems to give the best visualization, and why?
For barplot(), (and other plots()) we can add several arguments to make the plot more presentable.
- The
colargument changes the color of the bars. xlabandylabdefine the labels for the x- and y-axes.maingives the plot a title.ylimcan be used to control the range of values displayed on the y-axis.
See ?barplot() for additional help and customizations.
Produce a nicely formatted bar plot that displays the number of mothers in each smoking category
We can use the syntax
boxplot(vector1, vector2)to make a side by side box plot. Create a side by side boxplot of the mother’s ages and the father’s ages. Which gender tends to be older?Run the command
tinyplot( ~ weight | habit, data = births, type = "hist"). Describe what this code does. Based on the graph, do you see any major differences between baby weights from smoking moms vs. non-smoking moms?