Question 1: There are 150 observations and 5 variables in the iris dataset.
library(dplyr)
library(tidyverse)
## ── Attaching packages ──────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble 3.2.1 ✔ stringr 1.5.1
## ✔ readr 2.1.5 ✔ forcats 0.5.2
## ✔ purrr 1.0.1
## ── Conflicts ─────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
iris <- iris
Question 2: There are 56 observations and 5 variables in the iris1 dataset.
iris1 <- iris %>%
filter(Species %in% c("versicolor", "virginica") & Sepal.Length > 6 & Sepal.Width > 2.5)
Question 3: There are 56 observations and 3 variables in the iris2 dataset.
iris2 <- iris1 %>%
select(Species, Sepal.Length, Sepal.Width)
Question 4:
iris3 <- iris2 %>%
arrange(desc(Sepal.Length))
head(iris3)
## Species Sepal.Length Sepal.Width
## 1 virginica 7.9 3.8
## 2 virginica 7.7 3.8
## 3 virginica 7.7 2.6
## 4 virginica 7.7 2.8
## 5 virginica 7.7 3.0
## 6 virginica 7.6 3.0
Question 5: There are 56 observations and 4 variables in the iris4 dataset.
iris4 <- iris3 %>%
mutate(Sepal.Area = (Sepal.Length*Sepal.Width))
Question 6:
iris5 <- iris4 %>%
summarise(avgLength = mean(Sepal.Length),
avgWidth = mean(Sepal.Width),
sampleSize = n())
print(iris5)
## avgLength avgWidth sampleSize
## 1 6.698214 3.041071 56
Question 7:
iris6 <- iris4 %>%
group_by(Species) %>%
summarise(avgLength = mean(Sepal.Length),
avgWidth = mean(Sepal.Width),
sampleSize = n())
print(iris6)
## # A tibble: 2 × 4
## Species avgLength avgWidth sampleSize
## <fct> <dbl> <dbl> <int>
## 1 versicolor 6.48 2.99 17
## 2 virginica 6.79 3.06 39
Question 8:
irisFinal <- iris %>%
filter(Species %in% c("versicolor", "virginica") & Sepal.Length > 6 & Sepal.Width > 2.5) %>%
select(Species, Sepal.Length, Sepal.Width) %>%
arrange(desc(Sepal.Length)) %>%
mutate(Sepal.Area = (Sepal.Length*Sepal.Width)) %>%
group_by(Species) %>%
summarise(avgLength = mean(Sepal.Length),
avgWidth = mean(Sepal.Width),
sampleSize = n())
print(irisFinal)
## # A tibble: 2 × 4
## Species avgLength avgWidth sampleSize
## <fct> <dbl> <dbl> <int>
## 1 versicolor 6.48 2.99 17
## 2 virginica 6.79 3.06 39
Question 9:
irisLong <- iris %>%
pivot_longer(cols = 1:4, names_to= "Measure", values_to= "Value")
print(irisLong)
## # A tibble: 600 × 3
## Species Measure Value
## <fct> <chr> <dbl>
## 1 setosa Sepal.Length 5.1
## 2 setosa Sepal.Width 3.5
## 3 setosa Petal.Length 1.4
## 4 setosa Petal.Width 0.2
## 5 setosa Sepal.Length 4.9
## 6 setosa Sepal.Width 3
## 7 setosa Petal.Length 1.4
## 8 setosa Petal.Width 0.2
## 9 setosa Sepal.Length 4.7
## 10 setosa Sepal.Width 3.2
## # ℹ 590 more rows