1

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
glimpse(iris)
## 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.…
## $ 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.…
## $ 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.…
## $ 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.…
## $ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…

2

iris1 <- filter(iris, Species == c('virginica', 'versicolor'), Sepal.Length > 6, Sepal.Width > 2.5)
glimpse(iris1)
## Rows: 28
## Columns: 5
## $ Sepal.Length <dbl> 6.4, 6.1, 6.7, 6.1, 6.1, 6.6, 6.7, 6.1, 6.2, 6.3, 7.1, 6.…
## $ Sepal.Width  <dbl> 3.2, 2.9, 3.1, 2.8, 2.8, 3.0, 3.0, 3.0, 2.9, 3.3, 3.0, 3.…
## $ Petal.Length <dbl> 4.5, 4.7, 4.4, 4.0, 4.7, 4.4, 5.0, 4.6, 4.3, 6.0, 5.9, 5.…
## $ Petal.Width  <dbl> 1.5, 1.4, 1.4, 1.3, 1.2, 1.4, 1.7, 1.4, 1.3, 2.5, 2.1, 2.…
## $ Species      <fct> versicolor, versicolor, versicolor, versicolor, versicolo…

3

iris2 <- select(iris1, Species, Sepal.Length, Sepal.Width)
glimpse(iris2)
## Rows: 28
## Columns: 3
## $ Species      <fct> versicolor, versicolor, versicolor, versicolor, versicolo…
## $ Sepal.Length <dbl> 6.4, 6.1, 6.7, 6.1, 6.1, 6.6, 6.7, 6.1, 6.2, 6.3, 7.1, 6.…
## $ Sepal.Width  <dbl> 3.2, 2.9, 3.1, 2.8, 2.8, 3.0, 3.0, 3.0, 2.9, 3.3, 3.0, 3.…

4

iris3 <- arrange(iris2, desc(Sepal.Length))
glimpse(iris3)
## Rows: 28
## Columns: 3
## $ Species      <fct> virginica, virginica, virginica, virginica, virginica, vi…
## $ Sepal.Length <dbl> 7.7, 7.7, 7.4, 7.1, 6.9, 6.8, 6.7, 6.7, 6.7, 6.7, 6.7, 6.…
## $ Sepal.Width  <dbl> 2.6, 2.8, 2.8, 3.0, 3.2, 3.0, 3.1, 3.0, 3.3, 3.1, 3.3, 3.…

5

iris4 <- mutate(iris3, Sepal.Area = Sepal.Length*Sepal.Width)
glimpse(iris4)
## Rows: 28
## Columns: 4
## $ Species      <fct> virginica, virginica, virginica, virginica, virginica, vi…
## $ Sepal.Length <dbl> 7.7, 7.7, 7.4, 7.1, 6.9, 6.8, 6.7, 6.7, 6.7, 6.7, 6.7, 6.…
## $ Sepal.Width  <dbl> 2.6, 2.8, 2.8, 3.0, 3.2, 3.0, 3.1, 3.0, 3.3, 3.1, 3.3, 3.…
## $ Sepal.Area   <dbl> 20.02, 21.56, 20.72, 21.30, 22.08, 20.40, 20.77, 20.10, 2…

6

iris5 <- summarize(iris4, meanSepal.Length = mean(Sepal.Length), meanSepal.Width = mean(Sepal.Width), N = n())
print(iris5)
##   meanSepal.Length meanSepal.Width  N
## 1            6.575        3.003571 28

7

irisGroups <- group_by(iris4, Species)
iris6 <- summarize(irisGroups, meanSepal.Length = mean(Sepal.Length), meanSepal.Width = mean(Sepal.Width), N = n())
glimpse(iris6)
## Rows: 2
## Columns: 4
## $ Species          <fct> versicolor, virginica
## $ meanSepal.Length <dbl> 6.333333, 6.689474
## $ meanSepal.Width  <dbl> 2.966667, 3.021053
## $ N                <int> 9, 19

8

iris7 <- iris %>%
  filter(Species == 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) %>%
  group_by(Species) %>%
  summarize(meanSepal.Length = mean(Sepal.Length), meanSepal.Width = mean(Sepal.Width), N = n())
glimpse(iris7)
## Rows: 2
## Columns: 4
## $ Species          <fct> versicolor, virginica
## $ meanSepal.Length <dbl> 6.333333, 6.689474
## $ meanSepal.Width  <dbl> 2.966667, 3.021053
## $ N                <int> 9, 19

9

newIris <- pivot_longer(iris, cols = 1:4, names_to = 'Measure', values_to = 'Value')
glimpse(newIris)
## Rows: 600
## Columns: 3
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa…
## $ Measure <chr> "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", …
## $ Value   <dbl> 5.1, 3.5, 1.4, 0.2, 4.9, 3.0, 1.4, 0.2, 4.7, 3.2, 1.3, 0.2, 4.…