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.
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
Extract the Sepal.Length column from the
iris dataset using $.
Are there any other ways to extract this column?
# Your code here
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
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
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
Run the summary() function on only the rows of
iris where Species is equal to
'setosa'
# Your code here
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
Save the dataset you just modified into a comma separated values file
named “myiris2.csv” using the function write.csv().
# Your code here
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
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
Use the built-in mean() function to find the mean of
Petal.Length in the iris dataset
# Your code here
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
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
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
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
Create an object called average_petal_length that stores
the mean of Petal.Length.
Then write an if statement that prints:
"Large average petal length" if the average is greater
than 3"Small average petal length" otherwiseHint:
if (SOME_LOGICAL_STATEMENT) {
DO_SOMETHING
}else{
DO_SOMETHINGELSE
}
# Your code here
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
Use a for loop to print the numbers from 1 to 10.
Hint:
for (VAR in VECTOR){
DO_SOMETHING_WITH_VAR
}
# Your code here
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
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
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
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.
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.
In the “Visual” tab of Rstudio, select “Insert” -> “Table…”.
Select 2 rows and 5 columns. The extra column is so you can add in the row labels yourself. If you keep the “include header labels” box, you won’t have to add an extra row.
Modify the table’s row/col labels to match the names of the
variables then fill in the values from the iris
summary.
| 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
##
##
##