load-in the necessary libraries

library(skimr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ ggplot2   3.5.1     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── 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

Q1. Read in and Inspect the data

data("iris")

skim(iris)
Data summary
Name iris
Number of rows 150
Number of columns 5
_______________________
Column type frequency:
factor 1
numeric 4
________________________
Group variables None

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
Species 0 1 FALSE 3 set: 50, ver: 50, vir: 50

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Sepal.Length 0 1 5.84 0.83 4.3 5.1 5.80 6.4 7.9 ▆▇▇▅▂
Sepal.Width 0 1 3.06 0.44 2.0 2.8 3.00 3.3 4.4 ▁▆▇▂▁
Petal.Length 0 1 3.76 1.77 1.0 1.6 4.35 5.1 6.9 ▇▁▆▇▂
Petal.Width 0 1 1.20 0.76 0.1 0.3 1.30 1.8 2.5 ▇▁▇▅▃
# There are 5 variables with 150 observations

Q2. Create a new data frame iris1 that contains only the species virginica and versicolor with sepal lengths longer than 6 cm and sepal widths longer than 2.5 cm. How many observations and variables are in the data set?

iris1 <- iris %>%
  group_by(Species) %>%
  filter(Sepal.Length > 6,
         Sepal.Width > 2.5)
  
skim(iris1)
Data summary
Name iris1
Number of rows 56
Number of columns 5
_______________________
Column type frequency:
numeric 4
________________________
Group variables Species

Variable type: numeric

skim_variable Species n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Sepal.Length versicolor 0 1 6.48 0.30 6.1 6.20 6.5 6.7 7.0 ▇▅▅▆▃
Sepal.Length virginica 0 1 6.79 0.52 6.1 6.40 6.7 7.2 7.9 ▇▆▂▃▃
Sepal.Width versicolor 0 1 2.99 0.16 2.8 2.90 3.0 3.1 3.3 ▇▃▁▃▃
Sepal.Width virginica 0 1 3.06 0.28 2.6 2.85 3.0 3.2 3.8 ▇▇▇▁▂
Petal.Length versicolor 0 1 4.58 0.25 4.0 4.40 4.6 4.7 5.0 ▁▅▅▇▂
Petal.Length virginica 0 1 5.70 0.52 4.8 5.35 5.6 6.0 6.9 ▅▇▆▃▂
Petal.Width versicolor 0 1 1.42 0.12 1.2 1.30 1.4 1.5 1.7 ▇▇▅▁▁
Petal.Width virginica 0 1 2.07 0.28 1.4 1.80 2.1 2.3 2.5 ▂▅▃▆▇
# My df has 5 variables with 56 observations

Q3. create a iris2 data frame from iris1 that contains only the columns for Species, Sepal.Length, and Sepal.Width. How many observations and variables are in the data set?

iris2 <- iris1 %>%
  select(Species, Sepal.Length, Sepal.Width)

skim(iris2)
Data summary
Name iris2
Number of rows 56
Number of columns 3
_______________________
Column type frequency:
numeric 2
________________________
Group variables Species

Variable type: numeric

skim_variable Species n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Sepal.Length versicolor 0 1 6.48 0.30 6.1 6.20 6.5 6.7 7.0 ▇▅▅▆▃
Sepal.Length virginica 0 1 6.79 0.52 6.1 6.40 6.7 7.2 7.9 ▇▆▂▃▃
Sepal.Width versicolor 0 1 2.99 0.16 2.8 2.90 3.0 3.1 3.3 ▇▃▁▃▃
Sepal.Width virginica 0 1 3.06 0.28 2.6 2.85 3.0 3.2 3.8 ▇▇▇▁▂
# My df has 3 variables with 56 observations

Q4. Create an iris3 data frame from iris2 that orders the observations from largest to smallest sepal length. Show the first 6 rows of this data set.

iris3 <- iris2 %>%
  arrange(desc(Sepal.Length))

head(iris3)
## # A tibble: 6 × 3
## # Groups:   Species [1]
##   Species   Sepal.Length Sepal.Width
##   <fct>            <dbl>       <dbl>
## 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  
## 6 virginica          7.6         3

Q5. Create an iris4 data frame from iris3 that creates a column with a sepal area (length * width) value for each observation. How many observations and variables are in the data set?

iris4 <- iris3 %>%
  mutate(Sepal.Area = Sepal.Length * Sepal.Width)

skim(iris4)
Data summary
Name iris4
Number of rows 56
Number of columns 4
_______________________
Column type frequency:
numeric 3
________________________
Group variables Species

Variable type: numeric

skim_variable Species n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Sepal.Length versicolor 0 1 6.48 0.30 6.10 6.20 6.50 6.70 7.00 ▇▅▅▆▃
Sepal.Length virginica 0 1 6.79 0.52 6.10 6.40 6.70 7.20 7.90 ▇▆▂▃▃
Sepal.Width versicolor 0 1 2.99 0.16 2.80 2.90 3.00 3.10 3.30 ▇▃▁▃▃
Sepal.Width virginica 0 1 3.06 0.28 2.60 2.85 3.00 3.20 3.80 ▇▇▇▁▂
Sepal.Area versicolor 0 1 19.39 1.58 17.08 18.20 19.14 20.77 22.40 ▆▇▃▆▃
Sepal.Area virginica 0 1 20.85 2.88 15.86 19.50 20.79 21.68 30.02 ▃▇▃▁▁
# my df has 4 variables with 56 observations

Q6. Create iris5 that calculates the average sepal length, the average sepal width, and the sample size of the entire iris4 data frame and print iris5

iris5 <- data.frame(average.sepal.length = mean(iris4$Sepal.Length),
          average.sepal.width = mean(iris4$Sepal.Width), 
          sample.size = nrow(iris4)) 
print(iris5)
##   average.sepal.length average.sepal.width sample.size
## 1             6.698214            3.041071          56

Q7. Finally, create iris6 that calculates the average sepal length, the average sepal width, and the sample size for each species of in the iris4 data frame and print iris6.

iris6 <- iris4 %>%
  group_by(Species) %>%
  summarise(average.sepal.length = mean(iris4$Sepal.Length),
          average.sepal.width = mean(iris4$Sepal.Width), 
          sample.size = n())
print(iris6)
## # A tibble: 2 × 4
##   Species    average.sepal.length average.sepal.width sample.size
##   <fct>                     <dbl>               <dbl>       <int>
## 1 versicolor                 6.70                3.04          17
## 2 virginica                  6.70                3.04          39

Q8. In these exercises, you have successively modified different versions of the data frame iris1 iris2 iris3 iris4 iris5 iris6. At each stage, the output data frame from one operation serves as the input fro the next. A more efficient way to do this is to use the pipe operator %>% from the tidyr package. See if you can rework all of your previous statements (except for iris5) into an extended piping operation that uses iris as the input and generates irisFinal as the output

irisFinal <- iris %>%
  select(Species, Sepal.Length, Sepal.Width) %>%
  group_by(Species) %>%
  filter(Sepal.Length > 6,
         Sepal.Width > 2.5) %>%
   arrange(desc(Sepal.Length)) %>%
  mutate(Sepal.Area = Sepal.Length * Sepal.Width) %>%
  summarise(average.sepal.length = mean(Sepal.Length),
          average.sepal.width = mean(Sepal.Width), 
          sample.size = n())

print(irisFinal)
## # A tibble: 2 × 4
##   Species    average.sepal.length average.sepal.width sample.size
##   <fct>                     <dbl>               <dbl>       <int>
## 1 versicolor                 6.48                2.99          17
## 2 virginica                  6.79                3.06          39

Create a ‘longer’ data frame using the original iris data set with three columns named “Species”, “Measure”, “Value”. The column “Species” will retain the species names of the data set. The column “Measure” will include whether the value corresponds to Sepal.Length, Sepal.Width, Petal.Length, or Petal.Width and the column “Value” will include the numerical values of those measurements.

iris_longer <- iris %>%
  pivot_longer(!c(Species),names_to = "Measure",  values_to = "value")

print(iris_longer)
## # 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