-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
237 lines (237 loc) · 8.03 KB
/
.Rhistory
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
setwd('~/workshops/r-novice-gapminder/')
gapminder <- read.csv("data/gapminder-FiveYearData.csv", header = T)
vignette(package=rmarkdown)
```{r results='hide'}
getwd()
setwd('/Users/jtdennis/workshops/win2016-gps-intro-R/')
mean(gapminder[gapminder$continent == c("Asia", "Americas","Africa"), "gdpPercap"])
gapminder[, gapminder[, c("year", country)]
]]]
gapminder[, gapminder[, c("year", 'country')]
gapminder[, gapminder[, c("year", 'country')]]]]
gapminder[, gapminder[, c("year", 'country')]]
gapminder[, c("year", 'country')]
head(gapminder[, c("year", 'country')])
year_country_gdp_br<- gapminder[, c("year", "country", "gdpPercap")]
head(year_country_gdp_br)
library("tidyr", lib.loc="~/Library/R/3.2/library")
?tidyr
??tidyr
year_country_gdp <- select(gapminder,year,country,gdpPercap)
library(dplyr)
year_country_gdp <- select(gapminder,year,country,gdpPercap)
View(year_country_gdp)
lsls
gap_wide <- read.csv('https://goo.gl/4xPTex', header=TRUE, stringsAsFactors = FALSE)
str(gap_wide)
gap_long <- gap_wide %>% gather(obstype_year,obs_values,starts_with('pop'),starts_with('lifeExp'),starts_with('gdpPercap'))
View(gap_long)
View(gap_wide)
gap_long <- gap_wide %>% gather(obstype_year, obs_values, -continent, -country)
str(gap_long)
gap_long <- gap_long %>% separate(obstype_year, into=c('obs_type', 'year'), sep='_')
gap_long$year <- as.integer(gap_long$year)
View(gap_long)
head(gap_long)
tail(gap_long)
gap_normal <- gap_long %>% spread(obs_type, obs_values)
dim(gap_normal)
dim(gapminder)
gap_normal <- gap_normal[, names(gapminder)]
all.equal(gap_normal, gapminder)
head(gap_normal)
head(gapminder)
head(gapminder)
gap_normal <- gap_long %>% spread(obs_type,obs_values)
dim(gap_normal)
gap_normal <- gap_normal[,names(gapminder)]
all.equal(gap_normal,gapminder)
View(gapminder)
head(gap_normal)
head(gapminder)
gap_normal <- gap_normal %>% arrange(country,continent,year)
head(gap_normal)
head(gapminder)
all.equal(gap_normal,gapminder)
library("ggvis", lib.loc="~/Library/R/3.2/library")
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
ggivs()
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
ggvis()
A <- c(1,1,1,2,2,2,3,3,3)
B <- c(1,0,0,1,0,0,1,0,0)
C <- c(8,7,6,8,7,8,9,9,11)
D <- data.frame(A,B,C)
D
D %>% group_by(A) %>%
filter(abs(C-C[B==1]))
D %>%
group_by(A) %>%
filter(abs(C - C[B == 1]) <= 1)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap))
View(year_country_gdp)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent="Asia")
summarize(mean_gdpPercap=mean(gdpPercap))
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent="Asia") %>%
summarize(mean_gdpPercap=mean(gdpPercap))
gdp_bycontinents_byyear <- gapminder %>%
filter(continent="Asia") %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap))
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia") %>%
summarize(mean_gdpPercap=mean(gdpPercap))
View(gdp_bycontinents_byyear)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas") %>%
summarize(mean_gdpPercap=mean(gdpPercap))
View(gdp_bycontinents_byyear)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap))
View(gdp_bycontinents_byyear)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
mutate(difference = mean_gdpPercap - lag(mean_gdpPercap))
View(gdp_bycontinents_byyear)
View(gdp_bycontinents_byyear)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
mutate(difference = abs(mean_gdpPercap - lag(mean_gdpPercap)))
View(gdp_bycontinents_byyear)
str(gdp_bycontinents_byyear)
??lag
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
mutate(difference = mean_gdpPercap - lag(mean_gdpPercap, default = NA))
View(gdp_bycontinents_byyear)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
mutate(diff = mean_gdpPercap - lag(mean_gdpPercap, default = mean_gdpPercap[1]))
View(gdp_bycontinents_byyear)
diff(gdp_bycontinents_byyear$mean_gdpPercap)
View(gdp_bycontinents_byyear)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
diff(gdp_bycontinents_byyear$mean_gdpPercap)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap)) %>%
abs(diff(gdp_bycontinents_byyear$mean_gdpPercap))
abs(diff(gdp_bycontinents_byyear$mean_gdpPercap))
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap))
yr2002GdpPercapConti <- gdp_bycontinents_byyear %>%
filter(year==2002)
yr2002AsiaGdpPercap <- yr2002GdpPercapConti %>%
filter(continent=="Asia")
yr2002AmericasGdpPercap <- yr2002GdpPercapConti %>%
filter(continent=="Americas")
abs(yr2002AsiaGdpPercap$mean_gdpPercap - yr2002AmericasGdpPercap$mean_gdpPercap)
rm(c(D, gap_long, gap_normal))
gdp_byasiaamer_byyear <- gapminder %>%
group_by(continent,year) %>%
filter(continent=="Asia" | continent == "Americas", year == "2002") %>%
summarize(mean_gdpPercap=mean(gdpPercap))
abs(diff(gdp_byasiaamer_byyear$mean_gdpPercap))
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap))
yr2002GdpPercapConti <- gdp_bycontinents_byyear %>%
filter(year==2002)
yr2002AsiaGdpPercap <- yr2002GdpPercapConti %>%
filter(continent=="Asia")
yr2002AmericasGdpPercap <- yr2002GdpPercapConti %>%
filter(continent=="Americas")
abs(yr2002AsiaGdpPercap$mean_gdpPercap - yr2002AmericasGdpPercap$mean_gdpPercap)
gapminder <- read.csv("https://goo.gl/BtBnPg", header = T)
yr_2002_highgdp <-gapminder %>%
group_by(continent, year) %>%
filter(year==2002) %>%
head()
gapminder %>%
group_by(continent, year) %>%
filter(year==2002) %>%
head()
gapminder %>%
group_by(continent, year) %>%
head()
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002")
head()
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002") %>%
head()
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002") %>%
summarise(val = max(gdpPercap)) %>%
head()
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002") %>%
summarise(val = max(gdpPercap)) %>%
arrange(gdpPercap)
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002") %>%
summarise(val = max(gdpPercap))
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002") %>%
summarise(val = max(gdpPercap)) %>%
arrange(val)
gdp_bycontinents_byyear <- gapminder %>%
group_by(continent,year) %>%
summarize(mean_gdpPercap=mean(gdpPercap))
yr2002GdpPercapConti <- gdp_bycontinents_byyear %>%
filter(year==2002)
yr2002ContiGdpPercap <- gdp_bycontinents_byyear %>%
filter(year==2002)
yr2002ContiGdpPercap$continent[which.max(yr2002ContiGdpPercap$mean_gdpPercap)]
yr2002ContiGdpPercap$continent[which.min(yr2002ContiGdpPercap$mean_gdpPercap)]
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002") %>%
#summarise(val = max(gdpPercap)) %>%
arrange(val)
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002")
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002")
summarise(val = max(gdpPercap)) %>%
arrange(val)
gapminder %>%
group_by(continent, year) %>%
filter(year == "2002")
summarise(val = max(gdpPercap)) %>%
arrange(val)