By the end of this assignment, you should be able to:
quantile() to find percentiles.R file..Rmd file.Tip: Use # for comments. Comments are notes for
yourself or others, and R ignores them.
In R, you store information in objects using the
assignment operator <-.
Check the contents of an object by typing its name:
## [1] 21
my_age with your own age.major with your field of study (as
text).enrolled with the value
TRUE.R works like a calculator:
## [1] 5
## [1] 5
## [1] 25
You can also use objects:
## [1] 26
study_hours <- 15. Add 5 more hours
to it.Vectors hold multiple values of the same type. We use
c() to create homogeneous, one dimensional vectors in
R.
## [1] 85 90 78 92 88
Find the mean of scores:
## [1] 86.6
grades with the numbers: 100, 95, 82,
76, 89.grades.grades.## [1] 100 90 82 76 89
In the code grades[2], the square brackets
[] are being used for indexing (subsetting).
grades is a vector.grades[2] means “give me the second element of the
vector grades.”If:
then:
returns:
## [1] 100 90 82 76 89 85 70 88
Math is applied element-by-element.
## [1] 105 95 87 81 94 90
## [1] 50.0 45.0 41.0 38.0 44.5 42.5
## [1] 110.0 99.0 90.2 83.6 97.9 93.5
grades to
100.92 at the end of
grades.grades with a new vector
makeup <- c(80, 85).grades by 5 points.grades.grades.Logical subsetting returns the elements that meet a condition.
## [1] FALSE TRUE FALSE TRUE TRUE
## [1] 90 92 88
## [1] 90
## [1] 85 78 88
We will use a dataset of stress scores (0–20 scale) from 20 psychology students.
# Installing and Loading Packages
# Packages are collections of additional functions created by other R users.
#
# For this assignment, we will use the `dplyr` package.
#
# You only need to install a package once on your computer, but you need to load it(using library() function) every time you start a new R session.
## Step 1: Install the package
# Run this code once in R.
install.packages("dplyr")library(dplyr) # loads dplyr package
freq_table <- as.data.frame(table(stress_scores)) # converts table into a dataframe
colnames(freq_table) <- c("Score", "Frequency")
freq_table <- freq_table %>%
mutate(Relative_Frequency = Frequency / sum(Frequency),
Cumulative_Frequency = cumsum(Frequency))
freq_table%>% mean?The pipe operator (%>%) means “and
then.” It takes the result of one step and passes it to the next
step.
Example:
round() so the percentages have one decimal
place.hist(stress_scores,
main = "Histogram of Stress Scores",
xlab = "Stress Score",
ylab = "Frequency",
col = "lightblue",
border = "black")You can control the number of bins using the breaks
argument.
hist(stress_scores,
breaks = 5,
main = "Histogram with 5 bins",
xlab = "Stress Score",
col = "orange",
border = "black")breaks = 10 and breaks = 15.coping <- c("Exercise","Sleep","Sleep","Music","Music",
"Exercise","Exercise","Food","Music","Music",
"Sleep","Food","Exercise","Music","Food",
"Exercise","Music","Music","Sleep","Exercise")
barplot(table(coping),
main = "Coping Strategies",
ylab = "Frequency",
col = "lightgreen")Remember:
In R, we can use quantile() to find percentiles.
## 25% 50% 75%
## 7.75 10.00 12.25
This gives the 25th, 50th (median), and 75th percentiles.
x = your datasetprobs = probabilities written as decimalsExamples:
0.25 = 25th percentile0.50 = 50th percentile (median)0.75 = 75th percentileYou can ask for many percentiles at once:
## 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
## 3.0 5.9 6.8 8.0 8.6 10.0 10.4 12.0 13.2 15.1 18.0
quantile() to find the 10th and 90th percentiles in
stress_scores.We will now use a new dataset so you can practice what you have learned.
The dataset below shows hours of sleep per night reported by 25 undergraduate students.
sleep_hours <- c(4.5, 5, 5.5, 6, 6.5, 6.5, 6.5, 7, 7,
7, 7.5, 7.5, 8, 8, 8, 8, 8.5, 9, 9,
9, 9.5, 10, 10, 10.5, 11)average_sleep_goal and assign
it the value 8.weekday_sleep using the first 5
values from sleep_hours.weekday_sleep.weekday_sleep.sleep_hours.sleep_hours data.Modify your histogram:
Make histograms of sleep_hours using:
breaks = 5breaks = 10breaks = 15Then answer:
Suppose the same 25 students were asked their favorite time to study.
study_time <- c("Morning","Morning","Afternoon","Evening","Evening",
"Morning","Afternoon","Afternoon","Evening","Morning",
"Afternoon","Evening","Evening","Morning","Afternoon",
"Evening","Evening","Morning","Morning","Afternoon",
"Evening","Evening","Afternoon","Morning","Evening")study_time.quantile() to find the 25th, 50th, and 75th
percentiles of the sleep_hours dataset.Knit the completed .Rmd file into HTML.
Upload both your .Rmd file and knitted HTML file to
Brightspace.
Make sure all code and output are visible in your submission.