Quiz 3 Instructions

Please complete the following questions and submit a file named Quiz3.R to Gradescope for autograding.

Remember:

  • Do not use global paths in you script. Instead, use setwd() interactively in the console, but do not forget to remove or comment out this part of the code before you submit. The directory structure of your machine is not the same as the one on Gradescope’s virtual machines.
  • Do not destroy or overwrite any variables in your program. I check them only after I have run your entire program from start to finish.
  • Check to make sure you do not have any syntax errors. Code that doesn’t run will get a very bad grade.
  • Make sure to name your submission Quiz3.R

Tip: before submitting, it might help to clear all the objects from your workspace, and then source your file before you submit it. This will often uncover bugs.

Question 1 [2pt]

Create the vector vec1 with the values shown below, using the function seq().

#>   [1]  1.1  1.3  1.5  1.7  1.9  2.1  2.3  2.5  2.7  2.9  3.1  3.3  3.5  3.7  3.9
#>  [16]  4.1  4.3  4.5  4.7  4.9  5.1  5.3  5.5  5.7  5.9  6.1  6.3  6.5  6.7  6.9
#>  [31]  7.1  7.3  7.5  7.7  7.9  8.1  8.3  8.5  8.7  8.9  9.1  9.3  9.5  9.7  9.9
#>  [46] 10.1 10.3 10.5 10.7 10.9 11.1 11.3 11.5 11.7 11.9 12.1 12.3 12.5 12.7 12.9
#>  [61] 13.1 13.3 13.5 13.7 13.9 14.1 14.3 14.5 14.7 14.9 15.1 15.3 15.5 15.7 15.9
#>  [76] 16.1 16.3 16.5 16.7 16.9 17.1 17.3 17.5 17.7 17.9 18.1 18.3 18.5 18.7 18.9
#>  [91] 19.1 19.3 19.5 19.7 19.9 20.1 20.3 20.5 20.7 20.9

Question 2 [2pt]

Find out how to use the colon operator : to create the vector vec2 with values below.

#>  [1]  8.5  7.5  6.5  5.5  4.5  3.5  2.5  1.5  0.5 -0.5 -1.5 -2.5 -3.5 -4.5 -5.5
#> [16] -6.5 -7.5 -8.5

Question 3 [2pt]

Find out how to use the function rep() and the input vector 1:3 to create the vector vec3 with the values below, which has twenty 1, ten 2, and forty 3.

#>  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
#> [39] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

Question 4 [2pt]

Use the function sum() to show that \(1 + 2 + \cdots + 1000 = 500500.\) That is, create vector vec4 with values \(1, 2, \cdots, 1000\). Then calculate the sum.

Question 5 [2pt]

Load the R package MASS and use the data frame cats. Obtain vectors sex and bwt from the data frame with sex <- cats$Sex, bwt <- cats$Bwt. Show the body weights of female cats by creating vector bwtF. How many female cats are there in the data set? Please save the length value in object len.