There are 150 observations and 5 variables in the dataset (iris).
library(tidyverse)
## ── Attaching core tidyverse packages ────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ 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
data(iris)
class(iris)
## [1] "data.frame"
There are 56 observations and 5 variables in the dataset (iris1).
iris1 <- iris%>%
filter(Species %in% c("virginica","versicolor"), Sepal.Length > 6.0, Sepal.Width > 2.5)
There are 56 observations and 3 variables in the dataset (iris2).
iris2 <- iris1%>%
select("Species","Sepal.Length","Sepal.Width")
iris3 <- iris2%>%
arrange(by=Sepal.Length)
head(iris3)
## Species Sepal.Length Sepal.Width
## 1 versicolor 6.1 2.9
## 2 versicolor 6.1 2.8
## 3 versicolor 6.1 2.8
## 4 versicolor 6.1 3.0
## 5 virginica 6.1 3.0
## 6 virginica 6.1 2.6
iris4 <- iris3%>%
mutate(Sepal.Area=Sepal.Length*Sepal.Width)
iris5 <- iris4%>%
summarize(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
iris6 <- iris4%>%
group_by(Species) %>%
summarize(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
irisFinal<- iris%>%
filter(Species %in% c("virginica","versicolor"), Sepal.Length > 6.0, Sepal.Width > 2.5)%>% #iris1
select("Species","Sepal.Length","Sepal.Width")%>% #iris2
arrange(by=Sepal.Length)%>% #iris3
mutate(Sepal.Area=Sepal.Length*Sepal.Width)%>% #iris4
group_by(Species) %>% #iris6
summarize(Avg.Sepal.Length=mean(Sepal.Length),Avg.Sepal.Width=mean(Sepal.Width),Sample.Size=n()) #iris6
print(irisFinal)
## # 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
iris%>%
pivot_longer(col=Sepal.Length:Petal.Width, names_to="Measure",values_to="Value")
## # 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