Module 1 Assignment- Lignell
pdf
keyboard_arrow_up
School
Michigan State University *
*We aren’t endorsed by this school
Course
201
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
2
Uploaded by SargentNeutronLeopard51
1/21/24, 4:22 PM
Module 1 Assignment
file:///C:/Users/jlign/Downloads/assignment1_blank.html
1/2
Module 1 Assignment
Hyerin Seo
Welcome to the first assignment! Please answer all the questions below by creating a .Rmd script with the
necessary lines of code to solve the problems. You need to download and load the corresponding .RData file from
D2L. There should be no extra lines of code in the script (i.e. nothing with mistakes or unnecessary commands).
Use comments (lines that begin with #) to break up the document by question, and to provide your answers. When
you are done, knit the document (as shown in lecture) and turn in the resulting PDF.
Load the data
load('m1_assignment.RData')
Question 1
1. Create an object called my_first_vector
. Assign this vector the values 5 through 20, using the :
operator.
my_first_vector <- c(5:20)
my_first_vector
## [1] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2. What is the length of this vector?
length(my_first_vector)
## [1] 16
my_second_vector <- 2*(my_first_vector)
my_second_vector
## [1] 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
length(my_second_vector)
## [1] 16
The length of the first vector is 16
3. Create an object called my_second_vector
, whose values are all double those of the first vector. Do not use
c()
or the :
operator.
my_second_vector <- 2*(my_first_vector)
4. What is the length of this vector? Does it make sense that they are the same?
1/21/24, 4:22 PM
Module 1 Assignment
file:///C:/Users/jlign/Downloads/assignment1_blank.html
2/2
length(my_second_vector)
16, yes it does make sense that they are the same because everything was multiplied by the same number
5. Create one last vector called super_vector
which is a combination of the first two. What is its length?
Super_vector <- c(my_first_vector, my_second_vector) length(super_vector)
The length of the super_vector is 32
Question 2
There is an object already loaded in your R session (assuming you correctly downloaded and opened the RMD
file) called prime_ministers
. In it are the first five Prime Ministers of Canada.
1. Using subsetting [ ]
, who was the fifth Prime Minster of Canada? prime_ministers[5] Mackenzie Bowell
2. Who were the second and third Prime Ministers of Canada? Answer this question with only one line of code.
Prime_minister[2:3]
3. Create a new object called pm_except_first
that takes all values except the first Prime Minister of Canada
(i.e. the first element of the prime_ministers
vector.). Print the objects value. > pm_except_first <-
prime_ministers[-1] > pm_except_first Alexander Mackenzie 2 John Abbott 3 John Thompson
4 Mackenzie Bowell 5
Question 3
There is another vector loaded called weather
. It contains the temperatures of each day of the week, starting on
Sunday.
1. I wrote this assignment too fast! I forgot the names for each day of the week. Create names for the vector
that tell us which day of the week each temperature corresponds to (again, the week starts on Sunday)
names(weather) <- c(“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”,
“Friday”, “Saturday”) weather Sunday Monday Tuesday 48 50 55 Wednesday
Thursday Friday 43 59 44 Saturday 51 2. What was the average temperature of that
week? mean(weather) [1] 50
3. Which day had the hottest temperature? The coldest? What was the temperatures of those days? The
hottest day was Thursday with a temperature of 59 degrees. The coldest day was Wednesday witha a
temperature of 43 degrees.
4. Create a barplot that illustrates the temperature over the week.
The title of the plot should read “Temperature of Each Day”
The y-axis label should read “Degrees”
Change the color of the bar to “lightgreen”
barplot(weather,main=“Temperature of Each Day”, xlab = “Days”, ylab= “Degrees”, col = “lightgreen”)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help