Quiz 7 Instructions

Please complete the following questions and submit a file named Quiz7.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 Quiz7.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

We will use iris data frame, which is a data frame in the R package datasets. Please use ?iris to find more information.

  1. [1pt] Please find the number of columns of iris and store the value to object ncol.iris.

  2. [1pt] Please show the last three rows of iris and save it as Last.3.

  3. [1pt] Save the data where the species is “setosa” to the object named Setosa.

  4. [1pt] Find the column names of iris and save the result to colnames.iris.

  5. [3pts] Find the mean Sepal.Length of each Species. Save the results as mean.setosa, mean.versicolor, and mean.virginica.

Question 2

  1. [1pt] Create a data frame named df_abcd as follows
#>     a b     c   d
#> 1 5.5 a  TRUE 1.1
#> 2 4.5 a FALSE 1.2
#> 3 3.5 b  TRUE 1.3
#> 4 2.5 b FALSE 1.4
#> 5 1.5 b  TRUE 1.5
  1. [1pt] Write a command that would give you the following data from df_abcd and save the result as df_ac.
#>     a     c
#> 1 5.5  TRUE
#> 2 4.5 FALSE
#> 3 3.5  TRUE
#> 4 2.5 FALSE
#> 5 1.5  TRUE
  1. [1pt] Add a new_column e <- c(1, 1, 1, 1, 1) to df_abcd and save the new data frame as df_new. (Please note that, do not overwrite df_abcd.)
#>     a b     c   d e
#> 1 5.5 a  TRUE 1.1 1
#> 2 4.5 a FALSE 1.2 1
#> 3 3.5 b  TRUE 1.3 1
#> 4 2.5 b FALSE 1.4 1
#> 5 1.5 b  TRUE 1.5 1