-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge-data-frame-Emberts.R
56 lines (43 loc) · 1.68 KB
/
challenge-data-frame-Emberts.R
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
### Zach's answers
### Challenge:
### 1. use cbind again to add the species names to the 'surveys' data.frame
### 2. use the help in R to understand what the 'paste()' function does ('paste'),
### and use it to create a vector called 'genus_species' that contains
### the genus and species names togher e.g., "Amphispiza bilineata",
### and add it to the species data.frame
### 3. use the help in R to understand what the 'merge' function does, and
### use it to create a new data frame that merges the content of the 'surveys'
### and 'speceis' data fames
### 4. Use this data set to answer the following:
### - how many birds habe been captured? (610)
### -how many speciemens of the genus Dipodomys have been captured?(16,527)
surveys <- read.csv(file="data/surveys.csv")
species <- read.csv(file="data/species.csv")
surveys_spid_index <- match(surveys$species_id, species$species_id)
head(surveys_spid_index)
surveys_species <- species$species[surveys_spid_index]
head(surveys_species)
surveys <- cbind(surveys, species=surveys_species)
head(surveys)
?paste
head(species)
gs <- paste(species$genus, species$species)
gs
species <- cbind(species, name=gs)
head(species)
?merge()
M <- merge(surveys, species)
head(M)
survey_Bird <- subset(M, taxa == "Bird")
survey_D <- subset(M, genus.x == "Dipodomys")
### adding rows
surveys_DO <- subset(surveys, species_id == "DO")
surveys_DM <- subset(surveys, species_id == "DM")
surveys_DODM <- rbind(surveys_DO, surveys_DM)
nrow(surveys_DO)
nrow(surveys_DM)
nrow(surveys_DODM)
### challenge: what is the difference with subset(surveys, species_id == "DO" | species_id == "DM") ?
### removing columns
surveys_noDate <- surveys[, -c(3:5)]
names(surveys_noDate)