This homework assignment focus on data manipulation in R. Complete these problems using the dplyr and tidyverse packages.
library(tidyverse)
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 ...
iris1 <- iris %>% filter(Species == c("versicolor", "virginica"), Sepal.Length > 6.0, Sepal.Width > 2.5)
str(iris1)
## 'data.frame': 28 obs. of 5 variables:
## $ Sepal.Length: num 7 6.9 6.5 6.3 6.6 6.4 6.8 6.7 6.3 7.6 ...
## $ Sepal.Width : num 3.2 3.1 2.8 3.3 2.9 2.9 2.8 3.1 2.9 3 ...
## $ Petal.Length: num 4.7 4.9 4.6 4.7 4.6 4.3 4.8 4.7 5.6 6.6 ...
## $ Petal.Width : num 1.4 1.5 1.5 1.6 1.3 1.3 1.4 1.5 1.8 2.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 3 3 ...
iris2 <- iris1 %>% select(Species, Sepal.Length, Sepal.Width)
str(iris2)
## 'data.frame': 28 obs. of 3 variables:
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 3 3 ...
## $ Sepal.Length: num 7 6.9 6.5 6.3 6.6 6.4 6.8 6.7 6.3 7.6 ...
## $ Sepal.Width : num 3.2 3.1 2.8 3.3 2.9 2.9 2.8 3.1 2.9 3 ...
iris3<-iris2%>% arrange(by=desc(Sepal.Length))
head(iris3)
## Species Sepal.Length Sepal.Width
## 1 virginica 7.9 3.8
## 2 virginica 7.7 3.8
## 3 virginica 7.7 3.0
## 4 virginica 7.6 3.0
## 5 virginica 7.3 2.9
## 6 virginica 7.2 3.6
iris4 <- iris3 %>% mutate(iris3, Sepal.Area=Sepal.Length * Sepal.Width)
str(iris4)
## 'data.frame': 28 obs. of 4 variables:
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 3 3 3 3 3 3 3 3 2 2 ...
## $ Sepal.Length: num 7.9 7.7 7.7 7.6 7.3 7.2 7.2 7.2 7 6.9 ...
## $ Sepal.Width : num 3.8 3.8 3 3 2.9 3.6 3.2 3 3.2 3.1 ...
## $ Sepal.Area : num 30 29.3 23.1 22.8 21.2 ...
iris5<-iris4%>% summarize(meanLength=mean(Sepal.Length),meanWidth=mean(Sepal.Width),Size=n())
print(iris5)
## meanLength meanWidth Size
## 1 6.821429 3.078571 28
iris6 <- iris4 %>% group_by(Species) %>% summarize(av_mean_SepalLength=mean(iris4$Sepal.Length), av_mean_SepalWidth=mean(iris4$Sepal.Width), number=n())
print(iris6)
## # A tibble: 2 × 4
## Species av_mean_SepalLength av_mean_SepalWidth number
## <fct> <dbl> <dbl> <int>
## 1 versicolor 6.82 3.08 8
## 2 virginica 6.82 3.08 20
irisFinal <- iris %>%
filter(Species == c("versicolor", "virginica"), Sepal.Length > 6.0, Sepal.Width > 2.5) %>%
select(Species, Sepal.Length, Sepal.Width) %>%
group_by(Species) %>%
summarize(av_mean_SepalLength2=mean(Sepal.Length), av_mean_SepalWidth2=mean(Sepal.Width), number=n())
head(irisFinal)
## # A tibble: 2 × 4
## Species av_mean_SepalLength2 av_mean_SepalWidth2 number
## <fct> <dbl> <dbl> <int>
## 1 versicolor 6.65 3.01 8
## 2 virginica 6.89 3.10 20
iris_long <- iris %>%
pivot_longer(cols = Sepal.Length:Petal.Width, names_to="Measure", values_to="Value")
print(iris_long)
## # 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