-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise7_module2.Rmd
142 lines (99 loc) · 2.87 KB
/
exercise7_module2.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
## Exercise 7. Input / output and data selection.
Create the script "exercise7.R" and save it to the "Rcourse/Module2" directory: you will save all the commands of exercise 7 in that script.
<br>Remember you can comment the code using #.
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
getwd()
setwd("Rcourse/Module2")
setwd("~/Rcourse/Module2")
```
</details>
### Exercise 7
**1- Import the following file (use download.file() function) into your current working directory (Module2):**
https://data.cityofnewyork.us/resource/vfnx-vebw.csv
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
download.file(url="https://data.cityofnewyork.us/resource/vfnx-vebw.csv", destfile="vfnx-vebw.csv")
```
</details>
**2- Check that the file you download is present in the current directory (use dir())**
**3- Read this file into object squirrels (remember that the read.csv function exists)**
This data set contains the census of NYC central park squirrels!
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# with read.table
squirrels <- read.table("vfnx-vebw.csv", header=T, sep=",", as.is=TRUE)
# read.csv is an option
squirrels <- read.csv("vfnx-vebw.csv")
```
</details>
**4- Select only "Juvenile" squirrels ("Juvenile" in the "age" column). How many squirrels do you get?**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
squirrels[squirrels$age == "Juvenile", ]
```
</details>
**5- Select squirrels if their primary fur color is "Cinnamon". How many squirrels do you get?**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
squirrels[squirrels$primary_fur_color == "Cinnamon", ]
```
</details>
**6- Select squirrels that are both "Juvenile" and "Cinnamon". How many squirrels do you find? Write this selection to a csv file.**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
sqJC <- squirrels[squirrels$age == "Juvenile" & squirrels$primary_fur_color == "Cinnamon", ]
write.csv(sqJC, "squirrels_Juvenile_Cinnamon.csv", quote=FALSE, row.names=FALSE)
```
</details>
**7- Back to the main "squirrels" data frame: what are the most populated hectares?**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# use table to count occurrences of terms
table(squirrels[, "hectare"])
# then sort to check the most populated
sort(table(squirrels[, "hectare"]))
```
</details>
**8- How many squirrels are located in hectare 41B ? (see columns "hectare" and "hectare_squirrel_number")**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
# select hectare 41B
hect41B <- squirrels[squirrels$hectare == "41B",]
# sum up values in the "hectare_squirrel_number" column
sum(hect41B[, "hectare_squirrel_number"])
```
</details>
**9- From the hectare 41B selected squirrels: what "primary fur color" does the squirrel found "chasing" displayed?**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
hect41B[hect41B$chasing == "true", "primary_fur_color"]
```
</details>