What lovely flowers (Crocus 2022)

Home page

Question 1

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~

Looks like there are 150 observations and 5 variables: sepal length and width, petal length and width, and species

Question 2

iris1 <- iris %>% filter(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~

iris1 still has the same 5 variables as iris, however only 56 observations.

Question 3

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

iris2 still has 56 rows, but only 3 variables: Species, Sepal.Length, and Sepal.Width

Question 4

iris3 <- iris2 %>% arrange(desc(Sepal.Length))
head(iris3,6)
##   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

Question 5

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

There are still 56 rows, and now 4 variables: Species, Sepal.Length, Sepal.Width, and Sepal.Area

Question 6

iris5 <- iris4 %>% summarise(Ave.Sepal.Length=mean(Sepal.Length),
                             Ave.Sepal.Width=mean(Sepal.Width),
                             Sample.Size=n())
iris5
##   Ave.Sepal.Length Ave.Sepal.Width Sample.Size
## 1         6.698214        3.041071          56

Question 7

iris6 <- iris4 %>% group_by(Species) %>% 
  summarise(Ave.Sepal.Length=mean(Sepal.Length),
            Ave.Sepal.Width=mean(Sepal.Width),
            Sample.Size=n())
iris6
## # A tibble: 2 x 4
##   Species    Ave.Sepal.Length Ave.Sepal.Width Sample.Size
##   <fct>                 <dbl>           <dbl>       <int>
## 1 versicolor             6.48            2.99          17
## 2 virginica              6.79            3.06          39

Question 8

iris6 <- iris %>% filter(Species%in%c('virginica','versicolor'),
                         Sepal.Length>6,
                         Sepal.Width>2.5) %>% 
                  select(!Petal.Length:Petal.Width) %>% 
                  arrange(desc(Sepal.Length)) %>% 
                  mutate(Sepal.Area=Sepal.Length*Sepal.Width) %>% 
                  group_by(Species) %>% 
                  summarise(Ave.Sepal.Length=mean(Sepal.Length),
                            Ave.Sepal.Width=mean(Sepal.Width),
                            Sample.Size=n())
iris6
## # A tibble: 2 x 4
##   Species    Ave.Sepal.Length Ave.Sepal.Width Sample.Size
##   <fct>                 <dbl>           <dbl>       <int>
## 1 versicolor             6.48            2.99          17
## 2 virginica              6.79            3.06          39

Question 9

longer <- iris %>% 
  pivot_longer(cols = Sepal.Length:Petal.Width,
               names_to = 'Measure',
               values_to = 'Value')
longer
## # A tibble: 600 x 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
## # ... with 590 more rows