-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmore_dplyr.R
178 lines (148 loc) · 5.22 KB
/
more_dplyr.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
## ------------------------------------------------------------------------
sites <- read.csv("http://www.epa.gov/sites/production/files/2014-01/nla2007_sampledlakeinformation_20091113.csv")
## ------------------------------------------------------------------------
names(sites)
## ------------------------------------------------------------------------
sites_sel<-sites %>%
select(SITE_ID,LAKENAME,VISIT_NO,SITE_TYPE,WSA_ECO9,AREA_HA,DEPTHMAX)
head(sites_sel)
## ------------------------------------------------------------------------
#Ascending is default
sites_sel %>%
arrange(DEPTHMAX) %>%
head()
#Descending need desc()
sites_sel %>%
arrange(desc(DEPTHMAX)) %>%
head()
#By more than one column
sites_sel %>%
arrange(WSA_ECO9,desc(DEPTHMAX))%>%
head()
## ------------------------------------------------------------------------
sites_sel %>%
filter(DEPTHMAX >= 50)
## ------------------------------------------------------------------------
sites_sel %>%
filter(WSA_ECO9 == "NAP", DEPTHMAX >= 50)
## ------------------------------------------------------------------------
sites_sel %>%
slice(c(1,2))
#or
sites_sel %>%
slice(seq(1,nrow(sites_sel),100))
## ------------------------------------------------------------------------
sites_sel %>%
rename(Ecoregion = WSA_ECO9, MaxDepth = DEPTHMAX)%>%
head()
## ------------------------------------------------------------------------
sites_sel %>%
distinct(WSA_ECO9)
#Returns the first row with the distinct value so order has an impact
sites_sel %>%
arrange(desc(DEPTHMAX))%>%
distinct(WSA_ECO9)
## ------------------------------------------------------------------------
set.seed(72)
#By Number
sites_sel %>%
sample_n(10)
#By Fraction
sites_sel %>%
sample_frac(0.01)
## ------------------------------------------------------------------------
#Add it to the other columns
sites_sel %>%
mutate(volume = ((10000*AREA_HA) * DEPTHMAX)/3)%>%
head()
#Create only the new column
sites_sel %>%
transmute(mean_depth = (((10000*AREA_HA) * DEPTHMAX)/3)/(AREA_HA*10000)) %>%
head()
## ------------------------------------------------------------------------
sites_sel %>%
summarize(avg_depth = mean(DEPTHMAX,na.rm=T),
n = n()) %>%
head()
## ------------------------------------------------------------------------
sites_sel %>%
group_by(WSA_ECO9)
## ------------------------------------------------------------------------
sites_sel %>%
group_by(WSA_ECO9) %>%
summarize(avg = mean(DEPTHMAX,na.rm = T),
std_dev = sd(DEPTHMAX, na.rm = T),
n = n())
## ------------------------------------------------------------------------
wq <- read.csv("http://www.epa.gov/sites/production/files/2014-10/nla2007_chemical_conditionestimates_20091123.csv")
wq_sel<-wq %>%
select(SITE_ID,VISIT_NO,CHLA,NTL,PTL,TURB)
head(wq_sel)
sites_sel <- sites_sel %>%
filter(SITE_TYPE == "PROB_Lake")
## ------------------------------------------------------------------------
dim(sites_sel)
dim(wq_sel)
## ------------------------------------------------------------------------
sites_wq <- left_join(sites_sel,wq_sel)
dim(sites_wq)
head(sites_wq)
## ------------------------------------------------------------------------
wq_sites <- right_join(sites_sel,wq_sel)
dim(wq_sites)
head(wq_sites)
## ------------------------------------------------------------------------
#First manufacture some differences
wq_samp <- wq_sel %>%
sample_frac(.75)
sites_samp <- sites_sel %>%
sample_frac(.75)
dim(wq_samp)
dim(sites_samp)
#Then the inner_join
sites_wq_in <- inner_join(sites_samp,wq_samp)
dim(sites_wq_in)
head(sites_wq_in)
## ------------------------------------------------------------------------
sites_wq_all <- full_join(sites_sel, wq_sel)
dim(sites_wq_all)
head(sites_wq_all)
## ------------------------------------------------------------------------
#We need to load up the RSQLite package
library(RSQLite)
#Then connect
nla_sqlite <- src_sqlite("nla2007.sqlite3")
nla_sqlite
#List Tables
src_tbls(nla_sqlite)
## ------------------------------------------------------------------------
#Get it all
sites_sqlite <- tbl(nla_sqlite,"sites")
wq_sqlite <- tbl(nla_sqlite,"wq")
#Use some SQL
sites_qry <- tbl(nla_sqlite,sql("SELECT * FROM sites WHERE VISIT_NO == 1"))
sites_qry
## ------------------------------------------------------------------------
sites_sel_sqlite <- sites_sqlite %>%
select(SITE_ID,LAKENAME,VISIT_NO,SITE_TYPE,WSA_ECO9,AREA_HA,DEPTHMAX)
## ------------------------------------------------------------------------
object.size(sites_sel)
object.size(sites_sel_sqlite)
## ------------------------------------------------------------------------
sites_sel_collect <- sites_sel_sqlite %>%
arrange(desc(AREA_HA))%>%
collect()
## ------------------------------------------------------------------------
#A Bootstrapped sample
ecor_depth_stats <- sites_sel_collect %>%
group_by(WSA_ECO9) %>%
sample_n(1000,replace=T) %>%
summarize(avg = mean(DEPTHMAX, na.rm=TRUE),
sd = sd(DEPTHMAX, na.rm=TRUE),
boot_n = n())
#And write back to the database
src_tbls(nla_sqlite)
copy_to(nla_sqlite,ecor_depth_stats)
src_tbls(nla_sqlite)
## ----echo=FALSE,messages=FALSE,warning=FALSE-----------------------------
db_drop_table(nla_sqlite$con,table="ecor_depth_stats")