Instructions

Complete each question in the R code chunk provided. This homework uses the built-in iris dataset, so you do not need to download any data.

Question 1: Inspecting a dataset

Display the first 10 rows of the iris dataset. Hint: The : operator offers a quick way to specify a range of numbers. a:b will create a sequence of numbers starting at a and ending on b

# Your code here

Question 2: Accessing a column

Extract the Sepal.Length column from the iris dataset using $.

Are there any other ways to extract this column?

# Your code here

Question 3: Accessing rows and columns by position

Display the value in the first row and first column of iris.

Display the value in the 2nd row and 3rd column of iris.

Display all measurements from the 3rd row of iris.

# Your code here

Question 4: Find the elements of a vector that match a condition

Create and display a logical vector that shows the rows of iris where Species is equal to 'setosa'. Hint: Use the built-in == logical operator.

# Your code here

Question 5: Accessing rows using a condition

Display only the rows of iris where Species is equal to 'setosa'. Use == to determine which rows match the search criteria, then display those rows.

# Your code here

Question 6: Summarizing a subset

Run the summary() function on only the rows of iris where Species is equal to 'setosa'

# Your code here

Question 7: Creating a new column

You can add a new column to an existing data.frame by referencing the new column’s name with $ and assigning something to it.

Create a new column called Sepal.Ratio equal to:

Sepal.Length / Sepal.Width

Then display the first 6 rows of the updated dataset.

# Your code here

Question 8: Saving a dataset

Save the dataset you just modified into a comma separated values file named “myiris2.csv” using the function write.csv().

# Your code here

Question 9: Loading a dataset

Load the dataset you just saved into a new variable called iris2 using the read.csv() function. Hint: Make sure the file you are trying to load is visible from the current working directory.

# Your code here

Question 10:

Use the dim() and summary() functions to find the dimensions and summaries of iris and iris2 from the previous question. Compare the two and describe any differences you see.

# Your code here

Question 11:

Use the built-in mean() function to find the mean of Petal.Length in the iris dataset

# Your code here

Question 12:

Use the built in mean() function to find the mean Petal.Length of just the rows where Species is equal to 'setosa'

# Your code here

Question 13:

The %in% operator is used to tell you which elements of a vector are equal to any elements of another vector.

Use the built-in mean() function to find the mean Petal.Length of just the rows where Species is equal to 'setosa' or 'versicolor'.

# example
mycolors = c("blue", "red", "yellow", "green", "black")
mycolors %in% c("red", "green")
## [1] FALSE  TRUE FALSE  TRUE FALSE
# Your code here

Question 14:

Split the iris data.frame into 3 separate datasets based on Species, then run the summary() function on each. Which species has the highest average Sepal.Width?

# Your code here

Question 15:

Find out which row in iris has the highest Sepal.Width, and display all the measurements from that row. Hint: Use a combination of the built-in max() function and the == logical operator. Take care not to use = for the comparison or you will accidentally overwrite your values!

# Your code here

Bonus 1: Using an if statement

Create an object called average_petal_length that stores the mean of Petal.Length.

Then write an if statement that prints:

Hint:

if (SOME_LOGICAL_STATEMENT) {
    DO_SOMETHING
}else{
    DO_SOMETHINGELSE
}
# Your code here

Bonus 2: Using ifelse()

Create a new column in iris called Long.Sepal.

This column should say "yes" if Sepal.Length is greater than 5, and "no" otherwise.

Display the first 10 rows of the updated dataset.

Hint: Type ?ifelse at the R console to get documentation on this function.

# Your code here

Bonus 3: Writing a for loop

Use a for loop to print the numbers from 1 to 10.

Hint:

for (VAR in VECTOR){
    DO_SOMETHING_WITH_VAR
}
# Your code here

Bonus 4: Looping over column names

Create a vector containing these column names:

c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")

Then use a for loop to print the mean of each column.

# Your code here

Bonus 5: Writing a simple function

Write a function called range_size() that takes a numeric vector as input and returns:

max(x) - min(x)

Use your function to find the range size of iris$Sepal.Length.

HINT:

FUNNAME <- function(PARAMETERS){
    FUNCTIONBODY
    LASTLINE_IS_WHAT_GETS_RETURNED
}
# Your code here

Bonus 6: Combining a function and a loop

Use your range_size() function from Bonus 5 inside a for loop to calculate the range size for each numeric column in iris.

The numeric columns are:

c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
# Your code here

Bonus 7: Add a floating table of contents

Modify the YAML header at the top of this .Rmd file so that the document knits to an HTML file with a floating table of contents.

Replace this part of the header:

output: html_document

With:

output:
  html_document:
    toc: true
    toc_float: true
    toc_depth: 2

After changing the YAML header, knit the document yourself to make sure the floating table of contents appears in the HTML output.

# No R code is needed for this question.
# Instead, edit the YAML header at the top of this .Rmd file,
# then knit the document to HTML.

Bonus 8: Adding a table

Use the Visual editor in Rstudio to add a 2 x 4 Table to this report and fill it in with the min and max values of the four numeric variables from the iris dataset.

sepal.length S.width P.len P.width
min 34 456 45 45
max 12 23 34 6
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
##