# 1dim(iris)
# --- OR ----str(iris)
# str gives more info... but technically all we needed was dimensions# 2# str(iris) from above gives this answer as well
Section 2
# 1pet_width <- iris[,4]
# ----- OR -----pet_width <- iris$Petal.Width # this is same thing as above# 2mean(pet_width)
# 3iris[2,3]
# 4sum(iris$Sepal.Width >3.2)
# 5sum(iris$Sepal.Width >3.2)/length(iris$Sepal.Width)
# ---- ORsum(iris$Sepal.Width >3.2)/nrow(iris)
# Note that because iris$Sepal.Width is a vector, we can use length.# nrow on a data frame gives a sort of "length" of a df# So, either will work# 6table(iris$Species)
# 7new_data <- iris[iris$Species !="setosa",]
# This one may have been tricky for you# != means not equal and the comma tells us that we want# the iris data frame for all rows where # iris$Species IS NOT EQUAL to "setosa"# to check if you did this right - table(new_data$Species)# 8 mean(new_data$Sepal.Length)
# 9new_data$large_petal_flag <-ifelse(new_data$Petal.Length >4.5, 1, 0)
# 10# sumsum(new_data$large_petal_flag)
# proportionsum(new_data$large_petal_flag)/length(new_data$large_petal_flag)
# --- OR ---mean(new_data$large_petal_flag)
# Notice that the mean of a binary variable is the proportion of '1' values present# So, both the above works better with mean being easier to read
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
# 1 & # 2fancy_iris <- iris %>%mutate(double_petal =2* Petal.Width) %>%# 1filter(double_petal >2.3,
double_petal <4)
# Note that to filter by multiple things, we can separate by a comma# If we wanted and "OR", we could've typed "|"# Also, filter keeps true values. So, to filter OUT the values specified,# we needed to reverse the signs# 3iris %>%group_by(Species) %>%summarise(max_Sepal.Length =max(Sepal.Length))