Work Hours
Everyday: 北京时间8:00 - 23:59
Module 1 Assignment
Answers
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. 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.
Question 1
- Create an object called first_vec. Assign this vector the values 5 through 12, using the : operator.
Print the elements of the vector by calling the object name. - What is the length of this vector?
- Create an object called sqrt_vec, whose values are all square roots of those of the first vector. Print
the elements of the vectors. - Create an object called second_vec, whose values are A to D, all in capital letters. Print the elements
of the vector. - Create an object called combined_vec, which is a combination of the first_vec and second_vec.
Print the elements in the positions of odd numbers by using index (e.g. 1st, 3rd, 5th, 7th, 9th and 11th
elements). - What is the type of this vector combined_vec? Does it make sense and why?
Question 2
The information below is percentages of votes cast to Kamala Harris in the 2024 US presidential election
from some states around Michigan. Use the information for the questions below.
• MN: 51.1%
• WI: 48.8%
• IL: 54.8%
• MI: 48.3%
• OH: 43.9%
• PA: 48.6%
• NY: 56.4% - Make a vector called harris_support with the 7 percentages given above. Include ONLY the NUM
BERS without the percentage symbols. - Assign the names for the vector elements with the names of each states. Print the vector to check
whether the names are put correctly.
1 - What was the average support rate of the seven states?
- Imagine that you do not know any of the percentages yet. Find out the support rate of Harris in
Michigan, using the index with the name “MI”. - Using logical index, print all the states with a support rate over 50%.
- Which state has the lowest support rate? Which state with the highest? What are the percentages of
those states? - Create a barplot that illustrates the data of harris_support
1) The title of the plot should read “Support for Harris by States” and
2) the y-axis label should read “Percent”.
3) Change the color of the bar to “Blue”.
2