-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
512 lines (512 loc) · 16.7 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
Year = factor(year(pubDate), levels = 2017:2014)) %>%
filter(!is.na(duration_minutes)) %>%
ggplot(aes(x = duration_minutes, y = Year)) +
geom_density_ridges() +
theme_set(theme_jk_ridge()) +
labs(x = "방송시간 (분)", y = "연도",
title = "지적 대화를 위한 넓고 얕은 지식",
subtitle = "연도별 방송시간 분포") +
scale_x_continuous(breaks = seq(0, 150, 30), limits = c(0, 150))
rb_df <- feed_to_df("http://www.wisdomhouse.kr/sns/redbooks/redbooks.xml")
load("~/Downloads/sensesensibility.rda")
head(sensesensibility)
head(sensesensibility, 20)
271+268+256+275+288+301+296+287+282+245+292+252+246+244+270+230+241+220+221
a <- c(2.4866, 2.4988, 2.4902, 2.4802)
b <- c(2.4866, 2.4849, 2.4802, 2.4800)
ifelse(a > b, 1, 0)
options()
Sys.getenv()
remove.packages("rJava")
options("java.home")
options("java.home"="/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home")
install.packages("rJava",,"http://rforge.net/",type="source")
install.packages("rJava")
library(rJava)
remove.packages("rJava")
options("java.home"="/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home")
install.packages("rJava",,"http://rforge.net/",type="source")
totalPop2010 <- 3831074
Years65_2010 <- 533533
Years85_2010 <- 77872
DWDA_65_2010 <- 19
DWDA_75_2010 <- 15
DWDA_85_2010 <- 12
DWDA_ALL_2010 <- 65
(DWDA_65_2010 + DWDA_75_2010) / (Years65_2010 - Years85_2010)
DWDA_85_2010 / Years85_2010
DWDA_ALL_2010 / totalPop2010
format(DWDA_ALL_2010 / totalPop2010, scientific = FALSE)
format((DWDA_65_2010 + DWDA_75_2010) / (Years65_2010 - Years85_2010), scientific = FALSE)
(DWDA_85_2010 / Years85_2010) / (DWDA_65_2010 + DWDA_75_2010) / (Years65_2010 - Years85_2010)
(DWDA_85_2010 / Years85_2010) / ((DWDA_65_2010 + DWDA_75_2010) / (Years65_2010 - Years85_2010))
(DWDA_85_2010 / Years85_2010) / (DWDA_ALL_2010 / totalPop2010)
10/1127
HIVAIDSPop <- 2735 + 3621 + 386 + 480
10/HIVAIDSPop
(10/HIVAIDSPop) / (DWDA_ALL_2010 / totalPop2010)
HIVAIDSPop
highschoolRate <- 0.898
bachelorRate <- 0.308
highschoolPop <- highschoolRate * totalPop2010
bachelorPop <- bachelorRate * totalPop2010
lowerEduPop <- totalPop2010 - highschoolPop
(4 / lowerEduPop) / (DWDA_ALL_2010 / totalPop2010)
(13 / (highschoolPop - bachelorPop)) / (DWDA_ALL_2010 / totalPop2010)
install.packages("rtweet")
library(rtweet)
Sys.getenv("TWITTER_APP")
appname <- "junhewk_roauth"
key <- "iFP1iQ7RNukQ4iBHT6zVUCaQZ"
secret <- "kaBayK9c0chgkfUizJFlfaxS6wLMVuJpg0dEuj5WUTKMo62LLD"
twitter_token <- create_token(
app = appname,
consumer_key = key,
consumer_secret = secret
)
rtweet_folks <- search_tweets("#rstats", n = 300)
dplyr::glimpse(rtweet_folks)
team_rstats <- search_twets("#rstats", n = 500)
team_rstats <- search_tweets("#rstats", n = 500)
users_data(team_rstats) %>%
head()
kylo_is_a_mole <- get_timeline("KyloR3n", n = 2000)
head(kylo_is_a_mole)
users_data(kylo_is_a_mole)
imwithher <- stream_tweets("HillaryClinton", timeout = 120)
head(imwithher)
head(users_data(imwithher))
harder_science <- search_users("social science", n = 500)
harder_science
users <- c("KimKardashian", "justinbieber", "taylorswift13",
"espn", "JoelEmbiid", "cstonehoops", "KUHoops",
"upshotnyt", "fivethirtyeight", "hadleywickham",
"cnn", "foxnews", "msnbc", "maddow", "seanhannity",
"potus", "epa", "hillaryclinton", "realdonaldtrump",
"natesilver538", "ezraklein", "annecoulter")
famous_tweeters <- lookup_users(users)
famous_tweeters
colbert_nation <- get_followers("stephanathome", n = 18000)
colbert_nation <- get_followers("stephenathome", n = 18000)
society_korean_users <- c("wonsoonpark", "moonriver365", "hcroh",
"urimal365", "OneFuturePower", "actormoon",
"ssaribi", "kennedian3", "GH_PARK", "Park_Youngsun")
politial_tweeters <- lookup_users(society_korean_users)
dplyr::glimpse(politial_tweeters)
colbert_nation <- get_followers("stephenathome", n = 18000)
library(rvest)
library(stringr)
temp_html <- read_html(temp)
temp <- "http://www.melon.com/chart/age/index.htm?chartType=YE&chartGenre=KPOP&chartDate=1964"
temp_html <- read_html(temp)
temp_html %>% html_table("lst50")
temp_html %>% html_node("lst50") %>% html_table
temp_html %>% html_node("lst50") %>% html_table()
temp_html %>% html_table()
temp_html %>% html_node("lst50")
temp_html
temp_html %>% html_nodes(xpath="//tr[class='lst50']")
temp_html %>% html_nodes(xpath = "//tr[@class='lst50']")
temp_html %>% html_nodes(xpath = "//tr")
temp_html <- read_html(temp)
temp_html %>% html_nodes(xpath = "//tr")
temp_html %>% html_nodes(xpath = "//div[class='tb_list type02 d_song_list']")
temp_html %>% html_nodes(xpath = "/div[class='tb_list type02 d_song_list']")
?html_nodes
temp_html %>% html_nodes("table")
temp_html
temp_html %>% html_text()
library(Rselenium)
install.packages("Rselenium")
install.packages("RSelenium")
library(RSelenium)
??RSelenium
library(RSelenium)
remDr <- remoteDriver(remoteServerAddr="localhost", port=4445L, browserName="chrome")
remDr$open()
remDr$getStatus()
remDr$navigate("http://www.melon.com/chart/age/index.htm?chartType=YE&chartGenre=KPOP&chartDate=1964")
?findElement
webElem <- remDr$findElement(using="xpath", value="//form[@id='frm']/table")
webElem
webElem$getPageSource
webElem$getPageSource()
webElem <- remDr$findElement(using="css selector", value="form.id.frm")
webElem <- remDr$findElement(using="css selector", value="form#frm")
webElem$getPageSource()
html(webElem$getPageSource([[1]])
html(webElem$getPageSource()[[1]])
read_html(webElem$getPageSource()[[1]])
library(rvest)
html(webElem$getPageSource()[[1]])
read_html(webElem$getPageSource()[[1]])
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_table()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div#wrap_song_info") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div#wrap_song_info")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap_song_info") %>%
html_text()
library(stringr)
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap_song_info") %>%
html_text() %>%
str_replace_all("[\\n|\\t]*", ",")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap_song_info") %>%
html_text() %>%
str_replace_all("[\\n|\\t]+", ",")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap_song_info") %>%
html_text() %>%
str_replace_all("[\\n|\\t]+", ",") %>%
str_split(",")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap a")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap a.btn btn_icon_detail")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap a.btn")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap a.btn") %>%
html_attr("onclick")
read_html("http://www.melon.com/chart/age/index.htm?chartType=YE&chartGenre=KPOP&chartDate=1964") %>%
html_node("form#frm table") %>%
html_nodes("div.wrap_song_info") %>%
html_text() %>%
str_replace_all("[\\n|\\t]+", ",") %>%
str_split(",")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.ellipsis span strong") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.ellipsis a.fc_mgray") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.'ellipsis rank02' a.fc_mgray") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.rank02 a.fc_mgray") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes(span.checkEllipsis a.fc_mgray") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("span.checkEllipsis a.fc_mgray") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("span.checkEllipsis") %>%
html_text()
seq(1964, 2016, 1)
song <- "http://www.melon.com/song/detail.htm?songId=4083218"
remDr$navigate(song)
webElem <- remDr$findElement(using = "uss selector", value = "div.wrap_lyric")
webElem <- remDr$findElement(using = "css selector", value = "div.wrap_lyric")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
html_text()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric")
?html_attr
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
html_children()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
html_attrs()
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
gsub(pattern = "<.*?>", replacement = "|")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
gsub(pattern = "<.*?>", replacement = "|") %>%
str_trim("both")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
gsub(pattern = "<.*?>", replacement = "|") %>%
str_split("|") %>%
str_trim("both")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
gsub(pattern = "<.*?>", replacement = "|") %>%
str_split("\\|") %>%
str_trim("both")
remDr$navigate("http://www.melon.com/chart/age/index.htm?chartType=YE&chartGenre=KPOP&chartDate=1964")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("form#frm table") %>%
html_nodes("div.wrap_song_info") %>%
html_node("div.rank01") %>%
html_text()
song_none <- "http://www.melon.com/song/detail.htm?songId=3003849"
remDr$navigate(song_none)
webElem <- remDr$findElement(using = "css selector", value = "div.wrap_lyric")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric")
read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
gsub(pattern = "<.*?>", replacement = "|") %>%
str_split("\\|") %>%
str_trim("both")
lyrics <- read_html(webElem$getPageSource()[[1]]) %>%
html_node("div.lyric") %>%
gsub(pattern = "<.*?>", replacement = "|") %>%
str_split("\\|") %>%
str_trim("both")
is.na(lyrics)
a <- c("temp")
a
a <- c(a, NA)
a
install.packages("wdman")
install.packages("wdman")
devtools::install_github("johndharrison/wdman")
?UseMethod
?attrs
?attributes
library(tokenizers)
tokenize_ngrams(c("가", "나", "다"), n = 3L, n_min = 1L)
tokenize_ngrams(c("가", "나", "다"), n = 3L, n_min = 2L)
tokenize_ngrams(c("가", "나", "다"), n = 4L, n_min = 1L)
?tokenize_ngrams
song <- paste0("How many roads must a man walk down\n",
"Before you call him a man?\n",
"How many seas must a white dove sail\n",
"Before she sleeps in the sand?\n",
"\n",
"How many times must the cannonballs fly\n",
"Before they're forever banned?\n",
"The answer, my friend, is blowin' in the wind.\n",
"The answer is blowin' in the wind.\n")
tokenize_ngrams(song, n = 4)
tokenize_ngrams("가 나 다"), n = 4L, n_min = 1L)
tokenize_ngrams("가 나 다", n = 4L, n_min = 1L)
tokenize_ngrams("가 나 다", n = 3L)
tokenize_ngrams("가 나 다", n = 2L)
library(RmecabKo)
txt <- paste("설렌다 Me Likey Me Likey Likey",
"Likey Me Likey Likey Likey",
"두근두근두근 Heart Heart",
"Me Likey Me Likey Likey Likey",
"Me Likey Likey Likey",
"두근두근두근",
"자꾸 드러내고 싶지",
"자꾸만 사소한 것 하나까지 전부다",
"작은 화면 속에 내가",
"제일 예뻐 보이고파",
"아직은 감춰 이런 내 마음 꾹꾹",
"멋 부린다는 건 정말 귀찮은 거",
"그렇다고 절대 대충할 수가 없는걸",
"매일 가슴 뛰게 해",
"이건 네가 몰라야만 돼",
"그러면서 뻔뻔하게",
"BB크림 파파파 립스틱을 맘맘마",
"카메라에 담아볼까 예쁘게",
"이거 보면 웃어줘 그리고 꼭 눌러줘",
"저 밑에 앙증맞고",
"새빨간 Heart Heart",
"근데 좋아요란 말은 뻔해",
"내 맘 표현하기엔 부족한데",
"근데 좋아요 잠도 못 자도",
"지각하게 돼도 좋은걸")
token_morph(txt)
txt <- paste("자꾸 드러내고 싶지",
"자꾸만 사소한 것 하나까지 전부다",
"작은 화면 속에 내가",
"제일 예뻐 보이고파",
"아직은 감춰 이런 내 마음 꾹꾹")
token_morph(txt)
txt <- paste("자꾸 드러내고 싶지",
"자꾸만 사소한 것 하나까지 전부다",
"작은 화면 속에 내가",
"제일 예뻐 보이고파",
"아직은 감춰 이런 내 마음 꾹꾹")
token_morph(txt)
token_words(txt)
token_nouns(txt)
knitr::opts_chunk$set(echo = TRUE)
txt <- paste("자꾸 드러내고 싶지",
"자꾸만 사소한 것 하나까지 전부다",
"작은 화면 속에 내가",
"제일 예뻐 보이고파",
"아직은 감춰 이런 내 마음 꾹꾹")
token_morph(txt)
token_words(txt)
token_nouns(txt)
token_ngrams(txt, n = 2L)
library(tidyverse)
library(tidytext)
library(RmecabKo)
library(stringr)
library(ggplot2)
setwd("/Volumes/Database/Data/R/lyrics")
songListLyrics <- read_csv("melon_ranking_lyrics_1964-2016.csv")
songListLyrics <- songListLyrics %>%
mutate(decade = floor(year / 10) * 10)
lyricsWords <- songListLyrics %>%
unnest_tokens(word, lyric, token = token_words)
lyticsWords %>%
count(word, sort = TRUE)
lyricsWords %>%
count(word, sort = TRUE)
wordsByDecade <- lyricsWords %>%
count(decade, word, sort = TRUE) %>%
ungroup()
wordsByDecade
tfIdf <- wordsByDecade %>%
bind_tf_idf(word, decade, n) %>%
arrange(desc(tf_idf))
tfIdf
tfIdf %>%
group_by(decade) %>%
top_n(12, tf_idf) %>%
ungroup() %>%
mutate(word = reorder(word, tf_idf)) %>%
ggplot(aes(word, tf_idf, fill = decade)) +
geom_col(show.legend = FALSE) +
face_wrap(~ decade, scales = "free") +
ylab("tf-idf") +
coord_flip()
tfIdf %>%
group_by(decade) %>%
top_n(12, tf_idf) %>%
ungroup() %>%
mutate(word = reorder(word, tf_idf)) %>%
ggplot(aes(word, tf_idf, fill = decade)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ decade, scales = "free") +
ylab("tf-idf") +
coord_flip()
tfIdf %>%
group_by(decade) %>%
top_n(12, tf_idf) %>%
ungroup() %>%
mutate(word = reorder(word, tf_idf)) %>%
ggplot(aes(word, tf_idf, fill = decade)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ decade, scales = "free") +
ylab("tf-idf") +
coord_flip() +
theme(axis.text.x = element_text(family = "Apple Gothic SD Neo"))
tfIdf %>%
group_by(decade) %>%
top_n(12, tf_idf) %>%
ungroup() %>%
mutate(word = reorder(word, tf_idf)) %>%
ggplot(aes(word, tf_idf, fill = decade)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ decade, scales = "free") +
ylab("tf-idf") +
coord_flip() +
theme(axis.text.x = element_text(family = "Apple SD Gothic Neo"))
tfIdf %>%
group_by(decade) %>%
top_n(12, tf_idf) %>%
ungroup() %>%
mutate(word = reorder(word, tf_idf)) %>%
ggplot(aes(word, tf_idf, fill = decade)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ decade, scales = "free") +
ylab("tf-idf") +
coord_flip() +
theme(axis.text.y = element_text(family = "Apple SD Gothic Neo"))
library(widyr)
decadeCors <- wordsByDecade %>%
pairwise_cor(decade, word, n, sort = TRUE)
decadeCors
songListLyrics <- songListLyrics %>%
na_omit() %>%
mutate(decade = floor(year / 10) * 10)
lyricsWords <- songListLyrics %>%
unnest_tokens(word, lyric, token = token_words)
lyricsWords %>%
count(word, sort = TRUE)
wordsByDecade <- lyricsWords %>%
count(decade, word, sort = TRUE) %>%
ungroup()
wordsByDecade
tfIdf <- wordsByDecade %>%
bind_tf_idf(word, decade, n) %>%
arrange(desc(tf_idf))
tfIdf
tfIdf %>%
group_by(decade) %>%
top_n(12, tf_idf) %>%
ungroup() %>%
mutate(word = reorder(word, tf_idf)) %>%
ggplot(aes(word, tf_idf, fill = decade)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ decade, scales = "free") +
ylab("tf-idf") +
coord_flip() +
theme(axis.text.y = element_text(family = "Apple SD Gothic Neo"))
library(widyr)
decadeCors <- wordsByDecade %>%
pairwise_cor(decade, word, n, sort = TRUE)
decadeCors
?na.omit
songListLyrics <- songListLyrics %>%
na.omit() %>%
mutate(decade = floor(year / 10) * 10)
lyricsWords <- songListLyrics %>%
unnest_tokens(word, lyric, token = token_words)
lyricsWords %>%
count(word, sort = TRUE)
wordsByDecade <- lyricsWords %>%
count(decade, word, sort = TRUE) %>%
ungroup()
wordsByDecade
tfIdf <- wordsByDecade %>%
bind_tf_idf(word, decade, n) %>%
arrange(desc(tf_idf))
tfIdf
tfIdf %>%
group_by(decade) %>%
top_n(12, tf_idf) %>%
ungroup() %>%
mutate(word = reorder(word, tf_idf)) %>%
ggplot(aes(word, tf_idf, fill = decade)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ decade, scales = "free") +
ylab("tf-idf") +
coord_flip() +
theme(axis.text.y = element_text(family = "Apple SD Gothic Neo"))
library(widyr)
decadeCors <- wordsByDecade %>%
pairwise_cor(decade, word, n, sort = TRUE)
decadeCors
install.packages(ggraph)
install.packages(c("ggraph", "igraph"))
library(ggraph)
library(igraph)
decadeCors %>%
graph_from_data_frame() %>%
ggraph(layout = "fr") +
geom_edge_link(aes(alpha = correlation, width = correlation)) +
geom_node_point(size = 6, color = "lightblue") +
geom_node_text(aes(label = name), repel = TRUE) +
theme_void()