-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework #8.Rmd
100 lines (78 loc) · 4.03 KB
/
homework #8.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: 'homework #8'
author: "Maryam Nouri-Aiin"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Data manipulations using the dplyr package
This homework assignment focus on data manipulation in R. Complete these problems using the dplyr and tidyverse packages.
Examine the structure of the iris data set. How many observations and variables are in the data set?
```{r echo=TRUE, message=FALSE, warning=FALSE, paged.print=TRUE}
library(sqldf)
library(dplyr)
library(tidyverse)
data("iris")
class(iris)
head(iris)
glimpse(iris)
# variables and observations
str(iris)
```
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?
```{r echo=TRUE, message=FALSE, warning=FALSE}
iris1 <- filter(iris, Species %in% c("virginica", "versicolor") & Sepal.Length > 6, Sepal.Width > 2.5)
str(iris1)
head(iris1)
```
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?
```{r echo=TRUE, message=FALSE, warning=FALSE}
iris2 <- select (iris1, Species, Sepal.Length, Sepal.Width)
str(iris2)
head(iris2)
```
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.
```{r echo=TRUE, message=FALSE, warning=FALSE}
iris3 <- arrange(iris2, desc(Sepal.Length) )
head(iris3)
```
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?
```{r echo=TRUE, message=FALSE, warning=FALSE}
iris4 <- mutate ( iris3, Sepal.Area= Sepal.Length/Sepal.Width)
str(iris4)
head(iris4)
```
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.
```{r echo=TRUE, message=FALSE, warning=FALSE}
iris5 <- summarize(iris4, average.sepal.length = mean(Sepal.Length), average.sepal.width = mean(Sepal.Width), number= n() )
print(iris5)
```
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.
```{r echo=TRUE, message=FALSE, warning=FALSE}
iris6 <- iris4 %>%
group_by(Species) %>%
summarize(average.sepal.length = mean(Sepal.Length), average.sepal.width = mean(Sepal.Width), number= n() )
print(iris6)
```
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 fro 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.
```{r echo=TRUE, message=FALSE, warning=FALSE}
irisFinal <- iris %>%
filter(Species %in% c("virginica", "versicolor"),
Sepal.Length > 6,
Sepal.Width > 2.5) %>%
select(Species, Sepal.Length, Sepal.Width) %>%
arrange(desc(Sepal.Length)) %>%
head(6) %>%
mutate(Sepal.Area = Sepal.Length * Sepal.Width)
head(irisFinal)
```
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.
```{r echo=TRUE, message=FALSE, warning=FALSE}
longer <- iris %>%
pivot_longer(cols = starts_with("Sepal") | starts_with("Petal"),
names_to = "Measure",
values_to = "Value")
head(longer)
```