R dplyr Package

R dplyr interface

Using dplyr requires having access to a PostgreSQL server running BETYdb or installing your own.
Comprehensive documentation for the dplyr interface to databases is provided in the dplyr vignette

Connect to Database

1
library(dplyr)
2
library(data.table)
3
## connection to database
4
d <- list(host = 'localhost',
5
dbname = 'bety',
6
user = 'bety',
7
password = 'bety')
8
9
bety <- src_postgres(host = d$host, user = d$user, password = d$password, dbname = d$dbname)
Copied!

Query Miscanthus yield data

1
species <- tbl(bety, 'species') %>%
2
select(id, scientificname, genus) %>%
3
filter(genus == "Miscanthus") %>%
4
mutate(specie_id = id)
5
6
yields <-tbl(bety, 'yields') %>%
7
select(date, mean, site_id, specie_id)
8
9
sites <- tbl(bety, 'sites') %>%
10
select(id, sitename, city, country) %>%
11
mutate(site_id = id)
12
13
mxgdata <- inner_join(species, yields, by = 'specie_id') %>%
14
left_join(sites, by = 'site_id') %>%
15
select(-ends_with(".x"), -ends_with(".y")) %>% # drops duplicate rows
16
collect()
Copied!

Yield data with experimental treatments

Here we query Miscanthus and Switchgrass yield data along with planting, irrigation, and fertilization rates in order to update teh meta-analysis originally performed by Heaton et al (2004).
1
## query and join tables
2
species <- tbl(bety, 'species') %>%
3
select(id, scientificname, genus) %>%
4
rename(specie_id = id)
5
6
sites <- tbl(bety, sql(
7
paste("select id as site_id, st_y(st_centroid(sites.geometry)) AS lat,",
8
"st_x(st_centroid(sites.geometry)) AS lon,",
9
" sitename, city, country from sites"))
10
)
11
12
citations <- tbl(bety, 'citations') %>%
13
select(citation_id = id, author, year, title)
14
15
yields <- tbl(bety, 'yields') %>%
16
select(id, date, mean, n, statname, stat, site_id, specie_id, treatment_id, citation_id, cultivar_id) %>%
17
left_join(species, by = 'specie_id') %>%
18
left_join(sites, by = 'site_id') %>%
19
left_join(citations, by = 'citation_id')
20
21
managements_treatments <- tbl(bety, 'managements_treatments') %>%
22
select(treatment_id, management_id)
23
24
treatments <- tbl(bety, 'treatments') %>%
25
dplyr::mutate(treatment_id = id) %>%
26
dplyr::select(treatment_id, name, definition, control)
27
28
managements <- tbl(bety, 'managements') %>%
29
filter(mgmttype %in% c('fertilizer_N', 'fertilizer_N_rate', 'planting', 'irrigation')) %>%
30
dplyr::mutate(management_id = id) %>%
31
dplyr::select(management_id, date, mgmttype, level, units) %>%
32
left_join(managements_treatments, by = 'management_id') %>%
33
left_join(treatments, by = 'treatment_id')
34
35
nitrogen <- managements %>%
36
filter(mgmttype == "fertilizer_N_rate") %>%
37
select(treatment_id, nrate = level)
38
39
planting <- managements %>% filter(mgmttype == "planting") %>%
40
select(treatment_id, planting_date = date)
41
42
planting_rate <- managements %>% filter(mgmttype == "planting") %>%
43
select(treatment_id, planting_date = date, planting_density = level)
44
45
irrigation <- managements %>%
46
filter(mgmttype == 'irrigation')
47
48
irrigation_rate <- irrigation %>%
49
filter(units == 'mm', !is.na(treatment_id)) %>%
50
group_by(treatment_id, year = sql("extract(year from date)"), units) %>%
51
summarise(irrig.mm = sum(level)) %>%
52
group_by(treatment_id) %>%
53
summarise(irrig.mm.y = mean(irrig.mm))
54
55
irrigation_boolean <- irrigation %>%
56
collect %>%
57
group_by(treatment_id) %>%
58
mutate(irrig = as.logical(mean(level))) %>%
59
select(treatment_id, irrig = irrig)
60
61
irrigation_all <- irrigation_boolean %>%
62
full_join(irrigation_rate, copy = TRUE, by = 'treatment_id')
63
64
grass_yields <- yields %>%
65
filter(genus %in% c('Miscanthus', 'Panicum')) %>%
66
left_join(nitrogen, by = 'treatment_id') %>%
67
#left_join(planting, by = 'treatment_id') %>%
68
left_join(planting_rate, by = 'treatment_id') %>%
69
left_join(irrigation_all, by = 'treatment_id', copy = TRUE) %>%
70
collect %>%
71
mutate(age = year(date)- year(planting_date),
72
nrate = ifelse(is.na(nrate), 0, nrate),
73
SE = ifelse(statname == "SE", stat, ifelse(statname == 'SD', stat / sqrt(n), NA)),
74
continent = ifelse(lon < -30, 'united_states', ifelse(lon < 75, 'europe', 'asia')))
Copied!
Last modified 3yr ago