01. ํธ๋ผํ ์ฐ์ค๋ฌธ(trump.txt)๊ณผ ์ค๋ฐ๋ง ์ฐ์ค๋ฌธ(obama.txt)์ ๋์์ผ๋ก ๋น๋์๊ฐ 2ํ ์ด์ ๋จ์ด๋ฅผ ๋์์ผ๋ก ๋จ์ด๊ตฌ๋ฆ ์๊ฐํํ์์ค.
[๋จ๊ณ1], [๋จ๊ณ4] ~ [๋จ๊ณ8]
obama <- file(file.choose(), encoding="UTF-8")
obama_data <- readLines(obama)
str(obama_data) #1:496
obama_data[1:6]
๋ง๋ญ์น
myCorpus <- Corpus(VectorSource(obama_data))
myCorpus
inspect(myCorpus[100])
myCorpusPrepro <- tm_map(myCorpus, removePunctuation) # ๋ฌธ์ฅ๋ถํธ ์ ๊ฑฐ
myCorpusPrepro <- tm_map(myCorpusPrepro, removeNumbers) # ์์น ์ ๊ฑฐ
myCorpusPrepro <- tm_map(myCorpusPrepro, tolower) # ์๋ฌธ์ ๋ณ๊ฒฝ
stopwords('english')
myCorpusPrepro <-tm_map(myCorpusPrepro, removeWords, stopwords('english')) # ๋ถ์ฉ์ด์ ๊ฑฐ
์ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ ํ์ธ
myCorpusPrepro # Content: documents: 76
inspect(myCorpusPrepro[1:3])
๋จ์ด์ ๋ณ
(1) ํ๊ธ ๋จ์ด๊ธธ์ด 2์์ ~ 8์์ (ํ๊ธ 1๊ฐ 2byte)
myCorpusPrepro_term <- TermDocumentMatrix(myCorpusPrepro,
control=list(wordLengths=c(2,8)))
myCorpusPrepro_term
(2) Corpus -> ํ์๋ฌธ ๋ณํ : matrix -> data.frame ๋ณ๊ฒฝ
myTerm_df <- as.data.frame(as.matrix(myCorpusPrepro_term))
dim(myTerm_df) #1021 496
๋จ์ด ๋น๋์
(1) ๋จ์ด ๋น๋์ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
wordResult <- sort(rowSums(myTerm_df), decreasing=TRUE)
wordResult[1:10] # top10 ๋จ์ด
(2) ๋ถ์ฉ์ด ์ ๊ฑฐ
myStopwords = c(stopwords('english'), 'applause', 'cheers'); # ์ ๊ฑฐํ ๋ฌธ์ ์ถ๊ฐ
myCorpusPrepro <-tm_map(myCorpusPrepro, removeWords, myStopwords) # ๋ถ์ฉ์ด์ ๊ฑฐ
(3) ๋จ์ด ์ ๋ณ๊ณผ ํ์๋ฌธ ๋ณํ
myCorpusPrepro_term <- TermDocumentMatrix(myCorpusPrepro,
control=list(wordLengths=c(4,16))) # 2์์ ~ 8์์
(4) ๋ง๋ญ์น ๊ฐ์ฒด๋ฅผ ํ์๋ฌธ์ผ๋ก ๋ณํ
myTerm_df <- as.data.frame(as.matrix(myCorpusPrepro_term))
(5) ๋จ์ด ์ถํ ๋น๋์ ๊ตฌํ๊ธฐ
wordResult <- sort(rowSums(myTerm_df), decreasing=TRUE)
wordResult[1:10]
๋จ์ด ๊ตฌ๋ฆ ์๊ฐํ
(1) ๋จ์ด ์ด๋ฆ ์์ฑ -> ๋น๋์์ ์ด๋ฆ
myName <- names(wordResult)
(2) ๋จ์ด์ด๋ฆ๊ณผ ๋น๋์๋ก data.frame ์์ฑ
word.df <- data.frame(word=myName, freq=wordResult)
str(word.df) # word, freq ๋ณ์
head(word.df)
(3) ๋จ์ด ์์๊ณผ ๊ธ๊ผด ์ง์
pal <- brewer.pal(12,"Paired") # 12๊ฐ์ง ์์ pal <- brewer.pal(9,"Set1") # Set1~ Set3
ํฐํธ ์ค์ ์ธํ
: "๋ง์ ๊ณ ๋", "์์ธ๋จ์ฐ์ฒด B"
windowsFonts(malgun=windowsFont("๋ง์ ๊ณ ๋")) #windows
(4) ๋จ์ด ๊ตฌ๋ฆ ์๊ฐํ: ํฌ๊ธฐ,์ต์๋น๋์,์์,ํ์ ,์์,๊ธ๊ผด ์ง์
wordcloud(word.df$word, word.df$freq,
scale=c(3,1), min.freq=2, random.order=F,
rot.per=.1, colors=pal, family="malgun")

02. ๊ณต๊ณต๋ฐ์ดํฐ ์ฌ์ดํธ์์ ๊ด์ฌ๋ถ์ผ ๋ฐ์ดํฐ ์
์ ๋ค์ด๋ก๋ ๋ฐ์์ ๋น๋์๊ฐ 5ํ ์ด์ ๋จ์ด๋ฅผ ์ด์ฉํ์ฌ ๋จ์ด ๊ตฌ๋ฆ์ผ๋ก ์๊ฐํ ํ์์ค. ๊ณต๊ณต๋ฐ์ดํฐ ์ฌ์ดํธ : www.data.go.kr ๋๋ ๊ธฐํ ์ฌ์ดํธ
women <- file(file.choose(), encoding="UTF-8")
women_data <- readLines(women)
str(women_data)
์ง์ ๋ถ์ฉ์ด ์ ๊ฑฐ > ๋น๋์์์ ํ์ธํด๋ณด๊ธฐ
women_data = gsub("์ฌ์ฑ","",women_data)
women_data = gsub("์ฐ๋ฆฌ","",women_data)
๋จ์ด ์ถ์ถ
exNouns <- function(x) { #x๊ฐ ๋ฌธ์ฅ์ ๋ฐ์์ 1.character์ฒ๋ฆฌ(๋ฌธ์๋ณํ) 2.๋ช
์ฌ ์ถ์ถ 3.paste : ๊ณต๋ฐฑ์ผ๋ก ๋์ฒด
paste(extractNoun(as.character(x)), collapse=" ")
}
women_nouns <- sapply(women_data, exNouns)
women_nouns
์๋ฃ ์ ์ฒ๋ฆฌ
womenCorpus <- Corpus(VectorSource(women_nouns))
womenCorpus
๋ด์ฉ๋ณด๊ธฐ
inspect(data_unlist[1])
womenCorpusP <- tm_map(myCorpus, removePunctuation) # ๋ฌธ์ฅ๋ถํธ ์ ๊ฑฐ
womenCorpusP <- tm_map(womenCorpus, removeNumbers) # ์์น ์ ๊ฑฐ
womenCorpusP <- tm_map(womenCorpus, tolower) # ์๋ฌธ์ ๋ณ๊ฒฝ
inspect(womenCorpusP[1])
์ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ ํ์ธ
womenCorpusP
inspect(womenCorpusP[1])
๋จ์ด ์ ๋ณ
womenCorpusP_term <- TermDocumentMatrix(womenCorpusP,
control=list(wordLengths=c(4,16)))
womenCorpusP_term
ํ์๋ฌธ ๋ณํ
myTerm_df <- as.data.frame(as.matrix(womenCorpusP_term))
dim(myTerm_df) #548,1
str(myTerm_df)
๋น๋์ ๊ตฌํ๊ธฐ
wordResult <- sort(rowSums(myTerm_df), decreasing=TRUE)
wordResult[1:10]
์๊ฐํ
myName <- names(wordResult)
(2) ๋จ์ด์ด๋ฆ๊ณผ ๋น๋์๋ก data.frame ์์ฑ
word.df <- data.frame(word=myName, freq=wordResult)
str(word.df)
(3) ๋จ์ด ์์๊ณผ ๊ธ๊ผด ์ง์
pal <- brewer.pal(5,"Paired")
windowsFonts(malgun=windowsFont("๋ง์ ๊ณ ๋"))
(4) ๋จ์ด ๊ตฌ๋ฆ ์๊ฐํ: ํฌ๊ธฐ,์ต์๋น๋์,์์,ํ์ ,์์,๊ธ๊ผด ์ง์
wordcloud(word.df$word, word.df$freq,
scale=c(2,1), min.freq=5, random.order=F,
rot.per=.1, colors=pal, family="malgun")
myCorpusPrepro_term <- TermDocumentMatrix(myCorpusPrepro,
control=list(wordLengths=c(4,16))) # 2์์ ~ 8์์
myTerm_df <- as.data.frame(as.matrix(myCorpusPrepro_term))
wordResult <- sort(rowSums(myTerm_df), decreasing=TRUE)
wordResult

'๊ฐ์ธ๊ณต๋ถ > R' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| 28. R T๊ฒ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.12 |
|---|---|
| 24. R ์นด์ด์ ๊ณฑ๊ฒ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.08 |
| 21. R ํต๊ณ๊ธฐ๋ณธ๊ฐ๋ 2 (ํต๊ณ๋ถ์๋ชจ๋ธ) (0) | 2021.10.05 |
| 19. R EDA, ๋ฐ์ดํฐ์ ์ฒ๋ฆฌ ์ฐ์ต๋ฌธ์ (0) | 2021.10.03 |
| 16. ํต๊ณ๊ธฐ๋ณธ๊ฐ๋ (์ด์ฐํ๋ฅ ๋ถํฌ~๋ชจํ๊ท ์ถ์ ) (0) | 2021.09.30 |