/
slides2.Rpres
311 lines (223 loc) · 5 KB
/
slides2.Rpres
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<style>
.reveal .slides > sectionx {
top: -70%;
}
.reveal pre code.r {background-color: #ccF}
.section .reveal li {color:white}
.section .reveal em {font-weight: bold; font-style: "none"}
</style>
Text Analysis in R
========================================================
author: Wouter van Atteveldt
date: Session 2: Transforming Data & APIs
Course Overview
===
type:section
Thursday: Introduction to R
+ Organizing data
+ *Transforming data*
+ Accessing APIs from R
Friday: Corpus Analysis & Topic Modeling
Saturday: Machine Learning & Sentiment Analysis
Sunday: Semantic Networks & Grammatical Analysis
Transforming data
====
type:section
Combining data
Reshaping data
Combining data
=====
```{r, echo=F}
df = data.frame(id=1:3, age=c(14, 18, 24),
name=c("Mary", "John", "Luke"))
```
```{r}
cbind(df, country=c("nl", "uk", "uk"))
rbind(df, c(id=1, age=2, name="Mary"))
```
Merging data
===
```{r}
countries = data.frame(id=1:2, country=c("nl", "uk"))
merge(df, countries)
merge(df, countries, all=T)
```
Merging data
===
```{r, eval=F}
merge(data1, data2)
merge(data1, data2, by="id")
merge(data1, data2, by.x="id", by.y="ID")
merge(data1, data2, by="id", all=T)
merge(data1, data2, by="id", all.x=T)
```
Reshaping data
===
+ `reshape2` package:
+ `melt`: wide to long
+ `dcast`: long to wide (pivot table)
Melting data
===
```{r}
wide = data.frame(id=1:3,
group=c("a","a","b"),
width=c(100, 110, 120),
height=c(50, 100, 150))
wide
```
Melting data
===
```{r}
library(reshape2)
long = melt(wide, id.vars=c("id", "group"))
long
```
Casting data
===
```{r}
dcast(long, id + group ~ variable, value.var="value")
```
Casting data: aggregation
===
```{r}
dcast(long, group ~ variable, value.var = "value", fun.aggregate = max)
dcast(long, id ~., value.var = "value", fun.aggregate = mean)
```
Aggregation with `aggregate`
===
```{r}
aggregate(long["value"], long["group"], max)
```
`aggregate` vs `dcast`
===
Aggregate
+ One aggregation function
+ Multiple value columns
+ Groups go in rows (long format)
+ Specify with column subsets
Cast
+ One aggregation function
+ One value column
+ Groups go in rows or columns
+ Specify with formula (`rows ~ columns`)
Simple statistics
===
Vector properties
```{r, eval=F}
mean(x)
sd(x)
sum(x)
```
Basic tests
```{r, eval=F}
t.test(wide, width ~ group)
t.test(wide$width, wide$height, paired=T)
cor.test(wide$width, wide$height)
m = lm(long, width ~ group + height)
summary(m)
```
Interactive 2a
====
type: section
Transforming data in R
Course Overview
===
type:section
Thursday: Introduction to R
+ Organizing data
+ Transforming data
+ *Accessing APIs from R*
Friday: Corpus Analysis & Topic Modeling
Saturday: Machine Learning & Sentiment Analysis
Sunday: Semantic Networks & Grammatical Analysis
What is an API?
===
+ Application Programming Interface
+ Computer-friendly web page
+ Standardized requests
+ Structured response
+ json/ csv
+ Access directly (HTTP call)
+ Client library for popular APIs
Demo: APIs and HTTP requests
===
type: section
Package twitteR
===
```{r, eval=F}
install_github("geoffjentry/twitteR")
setup_twitter_oauth(...)
tweets = searchTwitteR("#Trump2016", resultType="recent", n = 10)
tweets = plyr::ldply(tweets, as.data.frame)
```
Package Rfacebook
===
```{r, eval=F}
install_github("pablobarbera/Rfacebook", subdir="Rfacebook")
fb_token = fbOAuth(fb_app_id, fb_app_secret)
p = getPage(page="nytimes", token=fb_token)
post = getPost(p$id[1], token=fb_token)
```
Package rtimes
====
```{r, eval=F}
install.packages("rtimes")
options(nytimes_as_key = nyt_api_key)
res = as_search(q="trump",
begin_date = "20160101",
end_date = '20160501')
arts = plyr::ldply(res$data,
function(x) c(headline=x$headline$main,
date=x$pub_date))
```
APIs and rate limits
===
+ Most APIs have access limits
+ Log on with key or token
+ Response size (page) limited to n results
+ Requests limited to n per hour/day
+ Some clients deal with this, some don't
+ See API and client documentation
Directly accessing APIs
===
+ Make HTTP requests directly from R
+ package `httr` (or `RCurl`)
+ Can access all web data source
+ Need to figure out authentication, structure, etc
Directly accessing APIs
===
```{r, eval=F}
domain = 'https://api.nytimes.com'
path = 'svc/search/v2/articlesearch.json'
url = paste(domain, path, url, sep='/')
query = list(`api-key`=key, q="clinton")
r = httr::GET(url, query=query)
status_code(r)
result = content(r)
result$response$docs[[1]]$headline
```
Interactive 2b
====
type: section
Accessing APIs
Hands-on 2
====
type: section
Break
Hand-outs:
+ Transforming data
+ Accesing APIs
+ Retrieve your own data
+ Bonus: modeling and visualizing
Mini-project:
Retrieve data about a topic of your interest
Course Overview
===
type:section
Thursday: Introduction to R
+ Organizing data
+ Transforming data
+ Accessing APIs from R
Friday: Corpus Analysis & Topic Modeling
Saturday: Machine Learning & Sentiment Analysis
Sunday: Semantic Networks & Grammatical Analysis