12.10 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.
Remember you can comment the code using #.

Answer
getwd()
setwd("Rcourse/Module2")
setwd("~/Rcourse/Module2")

12.10.1 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

Answer
download.file(url="https://data.cityofnewyork.us/resource/vfnx-vebw.csv", destfile="vfnx-vebw.csv")

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!

Answer
# 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")

4- Select only “Juvenile” squirrels (“Juvenile” in the “age” column). How many squirrels do you get?

Answer
squirrels[squirrels$age == "Juvenile", ]

5- Select squirrels if their primary fur color is “Cinnamon.” How many squirrels do you get?

Answer
squirrels[squirrels$primary_fur_color == "Cinnamon", ]

6- Select squirrels that are both “Juvenile” and “Cinnamon.” How many squirrels do you find? Write this selection to a csv file.

Answer
sqJC <- squirrels[squirrels$age == "Juvenile" & squirrels$primary_fur_color == "Cinnamon", ]
write.csv(sqJC, "squirrels_Juvenile_Cinnamon.csv", quote=FALSE, row.names=FALSE)

7- Back to the main “squirrels” data frame: what are the most populated hectares?

Answer
# use table to count occurrences of terms
table(squirrels[, "hectare"])

# then sort to check the most populated
sort(table(squirrels[, "hectare"]))

8- How many squirrels are located in hectare 41B ? (see columns “hectare” and “hectare_squirrel_number”)

Answer
# select hectare 41B
hect41B <- squirrels[squirrels$hectare == "41B",]
# sum up values in the "hectare_squirrel_number" column
sum(hect41B[, "hectare_squirrel_number"])

9- From the hectare 41B selected squirrels: what “primary fur color” does the squirrel found “chasing” displayed?

Answer
hect41B[hect41B$chasing == "true", "primary_fur_color"]