forked from ucsb-bren/ESM296-3W-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbest.Rmd
146 lines (103 loc) · 2.57 KB
/
bbest.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
142
143
144
145
146
---
title: "bbest"
author: "Ben Best"
date: "January 15, 2016"
output:
html_document:
theme: united
highlight: tango
---
## 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?

## 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)](http://www.statcan.gc.ca/pub/54-205-x/2011000/part-partie1-eng.htm):
```{r}
ports_bc = read.csv('data/bbest_ports-bc.csv')
summary(ports_bc)
```
## Data Wrangling
```{r, eval=FALSE}
# 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
```{r, eval=FALSE}
# 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:
```{r}
d = read.csv('../data/r-ecology/species.csv')
d
head(d)
summary(d)
```
## readr::read_csv
Better yet, try read_csv:
```{r}
library(readr)
d = read_csv('../data/r-ecology/species.csv')
d
head(d)
summary(d)
```
## dplry::tbl_df
Now convert to a dplyr table:
```{r}
library(readr)
library(dplyr)
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
head(d)
summary(d)
glimpse(d)
```
## dplyr loosely
### What year does species 'NL' show up in the surveys.csv?
```{r}
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())
d = read_csv('../data/r-ecology/species.csv') %>%
tbl_df()
d = tbl_df(read_csv('../data/r-ecology/species.csv'))
d
head(d)
summary(d)
glimpse(d)
```