1. Load the dataset

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()
data(iris)
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…

There are 150 observations and 5 variables: sepal length, sepal width, petal length, petal width, and species.

2. Create new data frame iris1

iris1 <- filter(iris, 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.…
## $ 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.…
## $ 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.…
## $ 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.…
## $ Species      <fct> versicolor, versicolor, versicolor, versicolor, versicolo…

There are 56 observations and 5 variables: sepal length, sepal width, petal length, petal width, and species.

3. Create new data frame iris2

iris2 <- select(iris1, Species, Sepal.Length, Sepal.Width)
glimpse(iris2)
## Rows: 56
## Columns: 3
## $ Species      <fct> versicolor, versicolor, versicolor, versicolor, versicolo…
## $ 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.…
## $ 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.…

There are 56 observations and 3 variables: sepal length, sepal width, and species.

4. Create new data frame iris3

iris3 <- arrange(iris2, 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

5. Create new data fram iris4

iris4 <- mutate(iris3, Sepal.Area=Sepal.Length*Sepal.Width)
glimpse(iris4)
## Rows: 56
## Columns: 4
## $ Species      <fct> virginica, virginica, virginica, virginica, virginica, vi…
## $ 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.…
## $ 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.…
## $ Sepal.Area   <dbl> 30.02, 29.26, 20.02, 21.56, 23.10, 22.80, 20.72, 21.17, 2…

There are 56 observations and 4 variables: sepal length, sepal width, sepal area, and species.

6. Calculation on iris4

iris5 <- summarize(iris4, 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

7. Calculation on groups from iris4

iris6 <- summarize(group_by(iris4, Species), 
                   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

8. Use pipe operation

pipe_iris6 <- 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) %>%
  group_by(Species) %>%
  summarize(avg_sepal_length=mean(Sepal.Length), 
            avg_sepal_width=mean(Sepal.Width), sample_size=n())

print(pipe_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

9. Create the longder data frame

longer_iris <- pivot_longer(iris, cols=Sepal.Length:Petal.Width,
                            names_to = "Measure",
                            values_to = "Value")
glimpse(longer_iris)
## 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.…
head(longer_iris)
## # A tibble: 6 × 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