Creating histograms in R
In this exercise, you will be analyzing your first real dataset by creating a histogram. Data in R are generally stored in data frames, which are simply tables with rows and columns. The data frame that you will use for this exercise is stored in the variable impact
and contains data on how concussions affect the memory capabilities of athletes. The data were collected by testing athletes before and after a concussion, using verbal and visual memory tests, among others. A score on one of these tests ranges from 0 (very bad) to 100 (perfect).
Creating a histogram of a variable in R is made easy with the hist()
function. To make your histogram more readable, it's good practice to add a title and labels to the x- and y-axis with the main
, xlab
and ylab
arguments to hist()
, respectively.
This exercise is part of the course
Intro to Statistics with R: Introduction
Exercise instructions
- Take a look at the data frame
impact
by printing it to the console. - Print out summary statistics for each variable using the
describe()
function. It returns a list of all variables together with some summary statistics. - Select the
verbal_memory_baseline
scores from the data frame and assign them to the variableverbal_baseline
. You can select one variable from a data frame by appending$variablename
to the name of the data frame. - Create a histogram of the
verbal_baseline
variable. Set"Distribution of verbal memory baseline scores"
as the title,"score"
as the x-axis label and"frequency"
as the y-axis label.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print the impact data frame
impact
# Use the describe() function to see some summary information per variable
# Select the variable 'verbal_memory_baseline' from the 'impact' data frame and assign it to the variable 'verbal_baseline'
verbal_baseline <- ___
# Plot a histogram of the verbal_baseline variable that you have just created