-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomework5.Rmd
116 lines (68 loc) · 3.51 KB
/
Homework5.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
100
101
102
103
104
105
106
107
108
---
title: "Homework5"
author: "Sam Rosenblatt"
date: "9/26/2018"
output: html_document
---
##Number 1
Examine the structure of the iris dataset. How many observations and variables are in the dataset?
```{r}
library(dplyr)
str(iris)
```
There are 150 observations and 5 Variables
## Number 2
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 dataset?
```{r}
iris1 <- filter(iris, Sepal.Length > 6, Sepal.Width > 2.5, Species %in% c("virginica", "versicolor"))
str(iris1)
```
There are 56 observations which match those criteria, and since we were subsetting observations by variable values, and not selecting variables, we still have 5 variables.
## Number 3
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 dataset?
```{r}
iris2 <- select(iris1, (c(Species, Sepal.Length, Sepal.Width)))
str(iris2)
```
Since we only subset the variables and not the observations, we still have 56 observations, but now we have only the 3 variables we explicitly kept
##Number 4
Create an iris3 data frame from iris2 that orders the observations from largest to smallest sepal length. Show the first 6 rows of this dataset.
```{r}
iris3 <- arrange(iris2, desc(Sepal.Length))
head(iris3)
```
## Number 5
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 dataset?
```{r}
iris4 <- mutate(iris3, Sepal.Area = Sepal.Length*Sepal.Width)
str(iris4)
```
We still have the 56 observations because we did not subset at all, but now we have added one variable so we have 4 variables
## Number 6
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}
iris5 <- summarize(iris4, avgSepalLength = mean(Sepal.Length), avgSepalWidth = mean(Sepal.Width), SampleSize = n())
print (iris5)
```
## Number 7
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}
iris6 <- iris4 %>%
group_by(Species) %>%
summarize(avgSepalWidth = mean(Sepal.Width), avgSepalLength = mean(Sepal.Length), SampleSize = n())
print(iris6)
```
##Number 8
In these exercises, you have successively modified different versions of the data frame iris1 iris1 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 into an extended piping operation that uses iris as the input and generates iris6 as the output.
Note: I beleive the instructions have two typos, Based on what I believe to make the most sense, I actually did it in the following order iris1, iris2, irs3, iris4, iris6.
```{r}
iris6 <- iris %>%
filter( Sepal.Length > 6, Sepal.Width > 2.5, Species %in% c("virginica", "versicolor")) %>%
select(c(Species, Sepal.Length, Sepal.Width))%>%
arrange(desc(Sepal.Length))%>%
mutate(Sepal.Area = Sepal.Length*Sepal.Width) %>%
group_by(Species) %>%
summarize(avgSepalWidth = mean(Sepal.Width), avgSepalLength = mean(Sepal.Length), SampleSize = n())
print(iris6)
```