/
slides1.Rpres
319 lines (238 loc) · 4.51 KB
/
slides1.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
312
313
314
315
316
317
318
<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 1: Managing data in R
Motivational Example
========================================================
```{r,echo=F, eval=F}
load("~/learningr/api_auth.rda")
twitteR::setup_twitter_oauth(tw_consumer_key, tw_consumer_secret, tw_token, tw_token_secret)
tweets = searchTwitteR("#bigdata", resultType="recent", n = 100)
saveRDS(tweets, file="ex_tweets.rds")
```
```{r, echo=F}
tweets = readRDS("ex_tweets.rds")
```
```{r, eval=F}
library(twitteR)
tweets = searchTwitteR("#bigdata", resultType="recent", n = 100)
tweets = plyr::ldply(tweets, as.data.frame)
```
```{r}
kable(head(tweets[c("id", "created", "text")]))
```
Motivational Example
======
```{r}
library(RTextTools)
library(corpustools)
dtm = create_matrix(tweets$text)
dtm.wordcloud(dtm, freq.fun = sqrt)
```
Course Overview
===
type:section
Thursday: Introduction to R
+ *Intro & Organizing data*
+ Transforming data
+ Accessing APIs from R
Friday: Corpus Analysis & Topic Modeling
Saturday: Machine Learning & Sentiment Analysis
Sunday: Semantic Networks & Grammatical Analysis
Introduction
===
+ Please introduce yourself
+ Background
+ What do you want to learn?
+ Experience with R / text / programming
Course Components
===
+ Each 3h session:
+ Lecture & Interactive sessions
+ Please interrupt me!
+ Break
+ Hands-on sessions
+ http://vanatteveldt.com
+ Slides, hand-outs, data
What is R?
===
+ Programming language
+ Statistics Toolkit
+ Open Source
+ Community driven
+ Packages/libraries
+ Including many text analysis libraries
Cathedral and Bazar
===
<img src="cath_bazar.jpg">
The R Ecosystem
===
+ R
+ RStudio
+ RMarkdown / RPresentation
+ Packages
+ CRAN
+ Github
Interactive 1a: What is R?
====
type: section
Installing and using packages
===
```{r, eval=F}
install.packages("plyr")
library(plyr)
plyr::rename
devtools::install_github("amcat/amcat-r")
```
Data types: vectors
===
```{r}
x = 12
class(x)
x = c(1, 2, 3)
class(x)
x = "a text"
class(x)
```
Data Frames
===
```{r}
df = data.frame(id=1:3, age=c(14, 18, 24),
name=c("Mary", "John", "Luke"))
df
class(df)
```
Selecting a column
===
```{r}
df$age
df[["age"]]
class(df$age)
class(df$name)
```
Useful functions
===
Data frames:
```{r, eval=F}
colnames(df)
head(df)
tail(df)
nrow(df)
ncol(df)
summary(df)
```
Vectors:
```{r, eval=F}
mean(df$age)
length(df$age)
```
Other data types
===
+ Data frame:
+ Rectangular data frame
+ Columns vectors of same length
+ (vetor always has one type)
+ List:
+ Contain anything (inc data frames, lists)
+ Elements arbitrary type
+ Matrix:
+ Rectangular
+ All cells same (primitive) type
Finding help (and packages)
===
+ Built-in documentation
+ CRAN package vignettes
+ Task views
+ Google (sorry...)
+ r mailing list
+ stack exchange
Organizing Data in R
===
type: section
Subsetting
Recoding & Renaming columns
Ordering
Subsetting
===
```{r}
df[1:2, 1:2]
df[df$id %% 2 == 1, ]
df[, c("id", "name")]
```
Subsetting: `subset` function
===
```{r}
subset(df, id == 1)
subset(df, id >1 & age < 20)
```
Recoding columns
===
```{r}
df2 = df
df2$age2 = df2$age + df2$id
df2$age[df2$id == 1] = NA
df2$id = NULL
df2$old = df2$age > 20
df2$agecat =
ifelse(df2$age > 20, "Old", "Young")
df2
```
Text columns
===
+ `character` vs `factor`
```{r}
df2=df
df2$name = as.character(df2$name)
df2$name[df2$id != 1] =
paste("Mr.", df2$name[df2$id != 1])
df2$name = toupper(df2$name)
df2$name = gsub("\\.\\s*", "_", df2$name)
df2[grepl("mr", df2$name, ignore.case = T), ]
```
Renaming columns
===
```{r}
df2 = df
colnames(df2) = c("ID", "AGE", "NAME")
colnames(df2)[2] = "leeftijd"
df2 = plyr::rename(df2, c("NAME"="naam"))
df2
```
Ordering
====
```{r}
df[order(df$age), ]
plyr::arrange(df, -age)
```
Accessing elements
====
+ Data frame
+ Select one column: `df$col`, ` df[["col"]]`,
+ Select columns: `df[c("col1" ,"col2")]`
+ Subset: `df[rows, columns]`
+ List:
+ Select one element: `l$el`, ` l[["el"]]`, `l[[1]]`
+ Select columns: `l[[1:3]]`
+ Matrix:
+ All cells same type
+ Subset: `m[rows, columns]`
Interactive 1b
====
type: section
Organizing Data
Hands-on 1
====
type: section
Break
Hand-outs:
+ Playing with data
+ Organizing data
+ Play with your own data!