Data manipulations using the dplyr package

library(sqldf)
library(dplyr)
library(tidyverse)
data("iris")

1. Examine the structure of the iris data set. How many observations and variables are in the data set?

str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

There are 150 observations and 5 variables.

2. 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 <- filter(iris, Species %in% c("virginica", "versicolor"), Sepal.Length > 6, Sepal.Width > 2.5)

There are 56 observations of 5 variables.

3. Now, 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 <- select(iris1, -(Petal.Length:Petal.Width))

There are 56 observations of 3 variables.

4. 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 <- arrange(iris2, by=desc(Sepal.Length))
head(iris3)
##   Sepal.Length Sepal.Width   Species
## 1          7.9         3.8 virginica
## 2          7.7         3.8 virginica
## 3          7.7         2.6 virginica
## 4          7.7         2.8 virginica
## 5          7.7         3.0 virginica
## 6          7.6         3.0 virginica

5. 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 <- mutate(iris3, Sepal.Area=Sepal.Length*Sepal.Width)

There are 56 observations of 4 variables.

6. 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 <- summarize(iris4, avgLength=mean(Sepal.Length), avgWidth=mean(Sepal.Width), Totalnumber=n())
print(iris5)
##   avgLength avgWidth Totalnumber
## 1  6.698214 3.041071          56

7. 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 <- summarize(iris4, avgLength=mean(Sepal.Length), avgWidth=mean(Sepal.Width), sizeVirginica=length(which(iris4$Species=="virginica")), sizeVersicolor=length(which(iris4$Species=="versicolor")))
print(iris6)
##   avgLength avgWidth sizeVirginica sizeVersicolor
## 1  6.698214 3.041071            39             17

8. 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 for 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 %>%
  filter(Species %in% c("virginica", "versicolor"), Sepal.Length > 6, Sepal.Width > 2.5) %>%
  select(-(Petal.Length:Petal.Width)) %>%
  arrange(by=desc(Sepal.Length)) %>%
  mutate(Sepal.Area=Sepal.Length*Sepal.Width)
head(irisFinal)
##   Sepal.Length Sepal.Width   Species Sepal.Area
## 1          7.9         3.8 virginica      30.02
## 2          7.7         3.8 virginica      29.26
## 3          7.7         2.6 virginica      20.02
## 4          7.7         2.8 virginica      21.56
## 5          7.7         3.0 virginica      23.10
## 6          7.6         3.0 virginica      22.80

9. 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.

irisLong <- iris %>%
  pivot_longer(cols=Sepal.Length:Petal.Width, names_to = "Measure", values_to = "Values")
head(irisLong)
## # A tibble: 6 × 3
##   Species Measure      Values
##   <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