Content

What is your burning environmental question that you’d like to address? Feel free to provide group project, dissertation, and/or personal interest. What’s the study area?

cool idea

Techniques

What techniques from the course do you think will be most applicable?

Data

What data have you already identified? Feel free to provide a link and/or details on the variables of interest.

Here is some data from Shipping in Canada (2011):

ports_bc = read.csv('data/bbest_ports-bc.csv')
summary(ports_bc)
##          name     int_ktons         dom_ktons          sum_ktons     
##  Alberni   :1   Min.   :   40.9   Min.   :    0.00   Min.   :  1018  
##  Beale Cove:1   1st Qu.:  854.3   1st Qu.:   39.65   1st Qu.:  1375  
##  Crofton   :1   Median : 1042.5   Median :  501.10   Median :  1864  
##  Howe Sound:1   Mean   :10380.6   Mean   : 1893.72   Mean   : 12274  
##  Kitimat   :1   3rd Qu.: 1609.7   3rd Qu.: 1761.88   3rd Qu.:  3918  
##  Mcneill   :1   Max.   :96516.3   Max.   :11058.90   Max.   :107575  
##  (Other)   :6                                                        
##       lon              lat             code  
##  Min.   :-130.3   Min.   :48.86   BCV    :1  
##  1st Qu.:-125.4   1st Qu.:49.25   CRO    :1  
##  Median :-124.5   Median :49.57   EVC    :1  
##  Mean   :-125.1   Mean   :50.27   HWS    :1  
##  3rd Qu.:-123.6   3rd Qu.:49.95   KTM    :1  
##  Max.   :-123.1   Max.   :54.31   NNO    :1  
##                                   (Other):6

Data Wrangling

# present working directory
getwd()

# change working directory
setwd('.')

# list files
list.files()

# list files that end in '.jpg'
list.files(pattern=glob2rx('*.jpg'))

# file exists
file.exists('test.png')

Install Packages

# Run this chunk only once in your Console
# Do not evaluate when knitting Rmarkdown

# list of packages
pkgs = c(
  'readr',        # read csv
  'readxl',       # read xls
  'dplyr',        # data frame manipulation
  'tidyr',        # data tidying
  'nycflights13', # test dataset of NYC flights for 2013
  'gapminder')    # test dataset of life expectancy and popultion

# install packages if not found
for (p in pkgs){
  if (!require(p, character.only=T)){
    install.packages(p)
  }
}

utils::read.csv

Traditionally, you would read a CSV like so:

d = read.csv('../data/r-ecology/species.csv')
d
##    species_id            genus         species    taxa
## 1          AB       Amphispiza       bilineata    Bird
## 2          AH Ammospermophilus         harrisi  Rodent
## 3          AS       Ammodramus      savannarum    Bird
## 4          BA          Baiomys         taylori  Rodent
## 5          CB  Campylorhynchus brunneicapillus    Bird
## 6          CM      Calamospiza     melanocorys    Bird
## 7          CQ       Callipepla        squamata    Bird
## 8          CS         Crotalus      scutalatus Reptile
## 9          CT    Cnemidophorus          tigris Reptile
## 10         CU    Cnemidophorus       uniparens Reptile
## 11         CV         Crotalus         viridis Reptile
## 12         DM        Dipodomys        merriami  Rodent
## 13         DO        Dipodomys           ordii  Rodent
## 14         DS        Dipodomys     spectabilis  Rodent
## 15         DX        Dipodomys             sp.  Rodent
## 16         EO          Eumeces       obsoletus Reptile
## 17         GS         Gambelia           silus Reptile
## 18         NL          Neotoma        albigula  Rodent
## 19         NX          Neotoma             sp.  Rodent
## 20         OL        Onychomys     leucogaster  Rodent
## 21         OT        Onychomys        torridus  Rodent
## 22         OX        Onychomys             sp.  Rodent
## 23         PB      Chaetodipus         baileyi  Rodent
## 24         PC           Pipilo       chlorurus    Bird
## 25         PE       Peromyscus        eremicus  Rodent
## 26         PF      Perognathus          flavus  Rodent
## 27         PG        Pooecetes       gramineus    Bird
## 28         PH      Perognathus        hispidus  Rodent
## 29         PI      Chaetodipus     intermedius  Rodent
## 30         PL       Peromyscus        leucopus  Rodent
## 31         PM       Peromyscus     maniculatus  Rodent
## 32         PP      Chaetodipus    penicillatus  Rodent
## 33         PU           Pipilo          fuscus    Bird
## 34         PX      Chaetodipus             sp.  Rodent
## 35         RF  Reithrodontomys      fulvescens  Rodent
## 36         RM  Reithrodontomys       megalotis  Rodent
## 37         RO  Reithrodontomys        montanus  Rodent
## 38         RX  Reithrodontomys             sp.  Rodent
## 39         SA       Sylvilagus       audubonii  Rabbit
## 40         SB         Spizella         breweri    Bird
## 41         SC       Sceloporus          clarki Reptile
## 42         SF         Sigmodon     fulviventer  Rodent
## 43         SH         Sigmodon        hispidus  Rodent
## 44         SO         Sigmodon    ochrognathus  Rodent
## 45         SS     Spermophilus       spilosoma  Rodent
## 46         ST     Spermophilus    tereticaudus  Rodent
## 47         SU       Sceloporus       undulatus Reptile
## 48         SX         Sigmodon             sp.  Rodent
## 49         UL           Lizard             sp. Reptile
## 50         UP           Pipilo             sp.    Bird
## 51         UR           Rodent             sp.  Rodent
## 52         US          Sparrow             sp.    Bird
## 53         ZL      Zonotrichia      leucophrys    Bird
## 54         ZM          Zenaida        macroura    Bird
head(d)
##   species_id            genus         species   taxa
## 1         AB       Amphispiza       bilineata   Bird
## 2         AH Ammospermophilus         harrisi Rodent
## 3         AS       Ammodramus      savannarum   Bird
## 4         BA          Baiomys         taylori Rodent
## 5         CB  Campylorhynchus brunneicapillus   Bird
## 6         CM      Calamospiza     melanocorys   Bird
summary(d)
##    species_id             genus         species        taxa   
##  AB     : 1   Chaetodipus    : 4   sp.      :10   Bird   :13  
##  AH     : 1   Dipodomys      : 4   hispidus : 2   Rabbit : 1  
##  AS     : 1   Reithrodontomys: 4   albigula : 1   Reptile: 9  
##  BA     : 1   Sigmodon       : 4   audubonii: 1   Rodent :31  
##  CB     : 1   Onychomys      : 3   baileyi  : 1               
##  CM     : 1   Peromyscus     : 3   bilineata: 1               
##  (Other):48   (Other)        :32   (Other)  :38

readr::read_csv

Better yet, try read_csv:

library(readr)

d = read_csv('../data/r-ecology/species.csv')
d
##    species_id            genus         species    taxa
## 1          AB       Amphispiza       bilineata    Bird
## 2          AH Ammospermophilus         harrisi  Rodent
## 3          AS       Ammodramus      savannarum    Bird
## 4          BA          Baiomys         taylori  Rodent
## 5          CB  Campylorhynchus brunneicapillus    Bird
## 6          CM      Calamospiza     melanocorys    Bird
## 7          CQ       Callipepla        squamata    Bird
## 8          CS         Crotalus      scutalatus Reptile
## 9          CT    Cnemidophorus          tigris Reptile
## 10         CU    Cnemidophorus       uniparens Reptile
## 11         CV         Crotalus         viridis Reptile
## 12         DM        Dipodomys        merriami  Rodent
## 13         DO        Dipodomys           ordii  Rodent
## 14         DS        Dipodomys     spectabilis  Rodent
## 15         DX        Dipodomys             sp.  Rodent
## 16         EO          Eumeces       obsoletus Reptile
## 17         GS         Gambelia           silus Reptile
## 18         NL          Neotoma        albigula  Rodent
## 19         NX          Neotoma             sp.  Rodent
## 20         OL        Onychomys     leucogaster  Rodent
## 21         OT        Onychomys        torridus  Rodent
## 22         OX        Onychomys             sp.  Rodent
## 23         PB      Chaetodipus         baileyi  Rodent
## 24         PC           Pipilo       chlorurus    Bird
## 25         PE       Peromyscus        eremicus  Rodent
## 26         PF      Perognathus          flavus  Rodent
## 27         PG        Pooecetes       gramineus    Bird
## 28         PH      Perognathus        hispidus  Rodent
## 29         PI      Chaetodipus     intermedius  Rodent
## 30         PL       Peromyscus        leucopus  Rodent
## 31         PM       Peromyscus     maniculatus  Rodent
## 32         PP      Chaetodipus    penicillatus  Rodent
## 33         PU           Pipilo          fuscus    Bird
## 34         PX      Chaetodipus             sp.  Rodent
## 35         RF  Reithrodontomys      fulvescens  Rodent
## 36         RM  Reithrodontomys       megalotis  Rodent
## 37         RO  Reithrodontomys        montanus  Rodent
## 38         RX  Reithrodontomys             sp.  Rodent
## 39         SA       Sylvilagus       audubonii  Rabbit
## 40         SB         Spizella         breweri    Bird
## 41         SC       Sceloporus          clarki Reptile
## 42         SF         Sigmodon     fulviventer  Rodent
## 43         SH         Sigmodon        hispidus  Rodent
## 44         SO         Sigmodon    ochrognathus  Rodent
## 45         SS     Spermophilus       spilosoma  Rodent
## 46         ST     Spermophilus    tereticaudus  Rodent
## 47         SU       Sceloporus       undulatus Reptile
## 48         SX         Sigmodon             sp.  Rodent
## 49         UL           Lizard             sp. Reptile
## 50         UP           Pipilo             sp.    Bird
## 51         UR           Rodent             sp.  Rodent
## 52         US          Sparrow             sp.    Bird
## 53         ZL      Zonotrichia      leucophrys    Bird
## 54         ZM          Zenaida        macroura    Bird
head(d)
##   species_id            genus         species   taxa
## 1         AB       Amphispiza       bilineata   Bird
## 2         AH Ammospermophilus         harrisi Rodent
## 3         AS       Ammodramus      savannarum   Bird
## 4         BA          Baiomys         taylori Rodent
## 5         CB  Campylorhynchus brunneicapillus   Bird
## 6         CM      Calamospiza     melanocorys   Bird
summary(d)
##   species_id           genus             species         
##  Length:54          Length:54          Length:54         
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##      taxa          
##  Length:54         
##  Class :character  
##  Mode  :character

dplry::tbl_df

Now convert to a dplyr table:

library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
d = read_csv('../data/r-ecology/species.csv')
d = tbl_df(d)

d = read_csv('../data/r-ecology/species.csv') %>%
  tbl_df()

d = tbl_df(read_csv('../data/r-ecology/species.csv'))
d
## Source: local data frame [54 x 4]
## 
##    species_id            genus         species    taxa
##         (chr)            (chr)           (chr)   (chr)
## 1          AB       Amphispiza       bilineata    Bird
## 2          AH Ammospermophilus         harrisi  Rodent
## 3          AS       Ammodramus      savannarum    Bird
## 4          BA          Baiomys         taylori  Rodent
## 5          CB  Campylorhynchus brunneicapillus    Bird
## 6          CM      Calamospiza     melanocorys    Bird
## 7          CQ       Callipepla        squamata    Bird
## 8          CS         Crotalus      scutalatus Reptile
## 9          CT    Cnemidophorus          tigris Reptile
## 10         CU    Cnemidophorus       uniparens Reptile
## ..        ...              ...             ...     ...
head(d)
## Source: local data frame [6 x 4]
## 
##   species_id            genus         species   taxa
##        (chr)            (chr)           (chr)  (chr)
## 1         AB       Amphispiza       bilineata   Bird
## 2         AH Ammospermophilus         harrisi Rodent
## 3         AS       Ammodramus      savannarum   Bird
## 4         BA          Baiomys         taylori Rodent
## 5         CB  Campylorhynchus brunneicapillus   Bird
## 6         CM      Calamospiza     melanocorys   Bird
summary(d)
##   species_id           genus             species         
##  Length:54          Length:54          Length:54         
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##      taxa          
##  Length:54         
##  Class :character  
##  Mode  :character
glimpse(d)
## Observations: 54
## Variables: 4
## $ species_id (chr) "AB", "AH", "AS", "BA", "CB", "CM", "CQ", "CS", "CT...
## $ genus      (chr) "Amphispiza", "Ammospermophilus", "Ammodramus", "Ba...
## $ species    (chr) "bilineata", "harrisi", "savannarum", "taylori", "b...
## $ taxa       (chr) "Bird", "Rodent", "Bird", "Rodent", "Bird", "Bird",...

dplyr loosely

What year does species ‘NL’ show up in the surveys.csv?

library(readr)
library(dplyr)

read_csv('../data/r-ecology/surveys.csv') %>%
  select(species_id, year) %>%
  #filter(species_id == 'NL') %>%
  group_by(species_id, year) %>%
  summarize(count = n())
## Source: local data frame [535 x 3]
## Groups: species_id [?]
## 
##    species_id  year count
##         (chr) (int) (int)
## 1          AB  1980     5
## 2          AB  1981     7
## 3          AB  1982    34
## 4          AB  1983    41
## 5          AB  1984    12
## 6          AB  1985    14
## 7          AB  1986     5
## 8          AB  1987    35
## 9          AB  1988    39
## 10         AB  1989    31
## ..        ...   ...   ...
d = read_csv('../data/r-ecology/species.csv') %>%
  tbl_df()

d = tbl_df(read_csv('../data/r-ecology/species.csv'))
d
## Source: local data frame [54 x 4]
## 
##    species_id            genus         species    taxa
##         (chr)            (chr)           (chr)   (chr)
## 1          AB       Amphispiza       bilineata    Bird
## 2          AH Ammospermophilus         harrisi  Rodent
## 3          AS       Ammodramus      savannarum    Bird
## 4          BA          Baiomys         taylori  Rodent
## 5          CB  Campylorhynchus brunneicapillus    Bird
## 6          CM      Calamospiza     melanocorys    Bird
## 7          CQ       Callipepla        squamata    Bird
## 8          CS         Crotalus      scutalatus Reptile
## 9          CT    Cnemidophorus          tigris Reptile
## 10         CU    Cnemidophorus       uniparens Reptile
## ..        ...              ...             ...     ...
head(d)
## Source: local data frame [6 x 4]
## 
##   species_id            genus         species   taxa
##        (chr)            (chr)           (chr)  (chr)
## 1         AB       Amphispiza       bilineata   Bird
## 2         AH Ammospermophilus         harrisi Rodent
## 3         AS       Ammodramus      savannarum   Bird
## 4         BA          Baiomys         taylori Rodent
## 5         CB  Campylorhynchus brunneicapillus   Bird
## 6         CM      Calamospiza     melanocorys   Bird
summary(d)
##   species_id           genus             species         
##  Length:54          Length:54          Length:54         
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##      taxa          
##  Length:54         
##  Class :character  
##  Mode  :character
glimpse(d)
## Observations: 54
## Variables: 4
## $ species_id (chr) "AB", "AH", "AS", "BA", "CB", "CM", "CQ", "CS", "CT...
## $ genus      (chr) "Amphispiza", "Ammospermophilus", "Ammodramus", "Ba...
## $ species    (chr) "bilineata", "harrisi", "savannarum", "taylori", "b...
## $ taxa       (chr) "Bird", "Rodent", "Bird", "Rodent", "Bird", "Bird",...