#Problem 1

library(tidyverse)

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…
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 ...
  1. There are 150 observations of 5 variables in the iris data set.

#Problem 2

iris1<-filter(iris, Species=='virginica'| Species=='versicolor' , Sepal.Length>6 ,  Sepal.Width>2.5)
str(iris1)
## 'data.frame':    56 obs. of  5 variables:
##  $ Sepal.Length: num  7 6.4 6.9 6.5 6.3 6.6 6.1 6.7 6.1 6.1 ...
##  $ Sepal.Width : num  3.2 3.2 3.1 2.8 3.3 2.9 2.9 3.1 2.8 2.8 ...
##  $ Petal.Length: num  4.7 4.5 4.9 4.6 4.7 4.6 4.7 4.4 4 4.7 ...
##  $ Petal.Width : num  1.4 1.5 1.5 1.5 1.6 1.3 1.4 1.4 1.3 1.2 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...
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…
  1. Iris1 contains 56 observations of 5 variables.

#Problem 3

iris2<-select(iris1,Species, Sepal.Length, Sepal.Width)
str(iris2)
## 'data.frame':    56 obs. of  3 variables:
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ Sepal.Length: num  7 6.4 6.9 6.5 6.3 6.6 6.1 6.7 6.1 6.1 ...
##  $ Sepal.Width : num  3.2 3.2 3.1 2.8 3.3 2.9 2.9 3.1 2.8 2.8 ...
  1. Iris2 contains 67 observations of 3 variables.

#Problem 4

iris3<-arrange(iris2, by=desc(Sepal.Length))
str(iris3)
## 'data.frame':    56 obs. of  3 variables:
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ Sepal.Length: num  7.9 7.7 7.7 7.7 7.7 7.6 7.4 7.3 7.2 7.2 ...
##  $ Sepal.Width : num  3.8 3.8 2.6 2.8 3 3 2.8 2.9 3.6 3.2 ...
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

#Problem 5

iris4<-mutate(iris3, sepalArea=Sepal.Width*Sepal.Length)
str(iris4)
## 'data.frame':    56 obs. of  4 variables:
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ Sepal.Length: num  7.9 7.7 7.7 7.7 7.7 7.6 7.4 7.3 7.2 7.2 ...
##  $ Sepal.Width : num  3.8 3.8 2.6 2.8 3 3 2.8 2.9 3.6 3.2 ...
##  $ sepalArea   : num  30 29.3 20 21.6 23.1 ...
  1. Iris4 contains 56 observations of 4 variables

#Problem 6

iris5<-summarize(iris4, meanSepalLength=mean(Sepal.Length), meanSepalWidth=mean(Sepal.Width),sampleSize=n())
print(iris5)
##   meanSepalLength meanSepalWidth sampleSize
## 1        6.698214       3.041071         56
str(iris5)
## 'data.frame':    1 obs. of  3 variables:
##  $ meanSepalLength: num 6.7
##  $ meanSepalWidth : num 3.04
##  $ sampleSize     : int 56

#Problem 7

iris6<- iris4%>%
  group_by(Species) %>% 
  summarize(MeanLength=mean(Sepal.Length), MeanWidth=mean(Sepal.Width), count=n() )
print(iris6)
## # A tibble: 2 × 4
##   Species    MeanLength MeanWidth count
##   <fct>           <dbl>     <dbl> <int>
## 1 versicolor       6.48      2.99    17
## 2 virginica        6.79      3.06    39

#Problem 8

IrisBigPipe<-iris %>% 
  filter(Species=='virginica'| Species=='versicolor' , Sepal.Length>6 ,  Sepal.Width>2.5) %>% 
  select(Species, Sepal.Length, Sepal.Width) %>% 
  arrange(iris2, by=desc(Sepal.Length)) %>% 
  mutate(iris3, sepalArea=Sepal.Width*Sepal.Length) %>% 
  group_by(Species) %>% 
  summarize(MeanLength=mean(Sepal.Length), MeanWidth=mean(Sepal.Width), count=n() )

str(IrisBigPipe)
## tibble [2 × 4] (S3: tbl_df/tbl/data.frame)
##  $ Species   : Factor w/ 3 levels "setosa","versicolor",..: 2 3
##  $ MeanLength: num [1:2] 6.48 6.79
##  $ MeanWidth : num [1:2] 2.99 3.06
##  $ count     : int [1:2] 17 39

#Problem 9

Long_Iris<-iris %>% 
  pivot_longer(cols=Sepal.Length:Petal.Width, names_to='Measure', values_to='value',values_drop_na = TRUE)
str(Long_Iris)
## tibble [600 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Measure: chr [1:600] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" ...
##  $ value  : num [1:600] 5.1 3.5 1.4 0.2 4.9 3 1.4 0.2 4.7 3.2 ...