Load required libraries
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ───────────────────────────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ─────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Question 1. Examine the structure of the iris dataset
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7…
## $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.4, 3.0, 3.0, 4.0, 4.4…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5…
## $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.2, 0.1, 0.1, 0.2, 0.4…
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa…
Question 2. Create iris1: Filter for species Virginica and
Versicolor with Sepal.Length > 6 and Sepal.Width > 2.5
iris1 <- iris %>%
filter(Species %in% c("virginica", "versicolor"),
Sepal.Length > 6,
Sepal.Width > 2.5)
glimpse(iris1)
## Rows: 56
## Columns: 5
## $ Sepal.Length <dbl> 7.0, 6.4, 6.9, 6.5, 6.3, 6.6, 6.1, 6.7, 6.1, 6.1, 6.4, 6.6, 6.8, 6.7, 6.7, 6.1…
## $ Sepal.Width <dbl> 3.2, 3.2, 3.1, 2.8, 3.3, 2.9, 2.9, 3.1, 2.8, 2.8, 2.9, 3.0, 2.8, 3.0, 3.1, 3.0…
## $ Petal.Length <dbl> 4.7, 4.5, 4.9, 4.6, 4.7, 4.6, 4.7, 4.4, 4.0, 4.7, 4.3, 4.4, 4.8, 5.0, 4.7, 4.6…
## $ Petal.Width <dbl> 1.4, 1.5, 1.5, 1.5, 1.6, 1.3, 1.4, 1.4, 1.3, 1.2, 1.3, 1.4, 1.4, 1.7, 1.5, 1.4…
## $ Species <fct> versicolor, versicolor, versicolor, versicolor, versicolor, versicolor, versic…
Question 3. Create iris2: Select only Species, Sepal.Length, and
Sepal.Width
iris2 <- iris1 %>%
select(Species, Sepal.Length, Sepal.Width)
glimpse(iris2)
## Rows: 56
## Columns: 3
## $ Species <fct> versicolor, versicolor, versicolor, versicolor, versicolor, versicolor, versic…
## $ Sepal.Length <dbl> 7.0, 6.4, 6.9, 6.5, 6.3, 6.6, 6.1, 6.7, 6.1, 6.1, 6.4, 6.6, 6.8, 6.7, 6.7, 6.1…
## $ Sepal.Width <dbl> 3.2, 3.2, 3.1, 2.8, 3.3, 2.9, 2.9, 3.1, 2.8, 2.8, 2.9, 3.0, 2.8, 3.0, 3.1, 3.0…
Question 4. Create iris3: Arrange by Sepal.Length in descending
order
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. Create iris4: Add a column for Sepal.Area (Sepal.Length
* Sepal.Width)
iris4 <- iris3 %>%
mutate(Sepal.Area = Sepal.Length * Sepal.Width)
glimpse(iris4)
## Rows: 56
## Columns: 4
## $ Species <fct> virginica, virginica, virginica, virginica, virginica, virginica, virginica, v…
## $ Sepal.Length <dbl> 7.9, 7.7, 7.7, 7.7, 7.7, 7.6, 7.4, 7.3, 7.2, 7.2, 7.2, 7.1, 7.0, 6.9, 6.9, 6.9…
## $ Sepal.Width <dbl> 3.8, 3.8, 2.6, 2.8, 3.0, 3.0, 2.8, 2.9, 3.6, 3.2, 3.0, 3.0, 3.2, 3.1, 3.2, 3.1…
## $ Sepal.Area <dbl> 30.02, 29.26, 20.02, 21.56, 23.10, 22.80, 20.72, 21.17, 25.92, 23.04, 21.60, 2…
Question 6. Create iris5: Compute overall mean Sepal.Length, mean
Sepal.Width, and sample size
iris5 <- iris4 %>%
summarise(Avg.Sepal.Length = mean(Sepal.Length),
Avg.Sepal.Width = mean(Sepal.Width),
Sample.Size = n())
print(iris5)
## Avg.Sepal.Length Avg.Sepal.Width Sample.Size
## 1 6.698214 3.041071 56
Question 7. Create iris6: Compute mean Sepal.Length, mean
Sepal.Width, and sample size grouped by Species
iris6 <- iris4 %>%
group_by(Species) %>%
summarise(Avg.Sepal.Length = mean(Sepal.Length),
Avg.Sepal.Width = mean(Sepal.Width),
Sample.Size = n())
print(iris6)
## # A tibble: 2 × 4
## Species Avg.Sepal.Length Avg.Sepal.Width Sample.Size
## <fct> <dbl> <dbl> <int>
## 1 versicolor 6.48 2.99 17
## 2 virginica 6.79 3.06 39
Question 8. Use piping to create irisFinal in one pipeline
irisFinal <- iris %>%
filter(Species %in% c("virginica", "versicolor"),
Sepal.Length > 6,
Sepal.Width > 2.5) %>%
select(Species, Sepal.Length, Sepal.Width) %>%
arrange(desc(Sepal.Length)) %>%
mutate(Sepal.Area = Sepal.Length * Sepal.Width)
head(irisFinal)
## Species Sepal.Length Sepal.Width Sepal.Area
## 1 virginica 7.9 3.8 30.02
## 2 virginica 7.7 3.8 29.26
## 3 virginica 7.7 2.6 20.02
## 4 virginica 7.7 2.8 21.56
## 5 virginica 7.7 3.0 23.10
## 6 virginica 7.6 3.0 22.80
Question 9. Create a longer data frame with columns: Species,
Measure, Value
iris_long <- iris %>%
pivot_longer(cols = -Species, names_to = "Measure", values_to = "Value")
print(iris_long)
## # 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