R如何从理货制作词云?
R How to make a word cloud from a tally?
这是我的计数的重复:
word_tally <- data.frame(scarred = c(1,1,0,0,0,0,0,0,0,0,0,0,0),
happy = c(0,0,1,0,0,0,0,0,0,0,0,0,0),
cheerful = c(0,0,0,1,0,0,0,0,0,0,0,0,0),
mad = c(0,0,0,0,1,1,1,1,1,0,0,0,0),
curious = c(0,0,0,0,0,0,0,0,0,1,1,1,1))
要制作词云,我似乎需要 1 个包含所有词的列。我怎样才能转换上述数据框来为词云制作这种类型的结构?
您可以获取长格式的数据并删除 value = 0
中的行。
library(dplyr)
tidyr::pivot_longer(word_tally, cols = everything(), names_to = "word") %>%
filter(value != 0) %>%
select(word)
# A tibble: 13 x 1
# word
# <chr>
# 1 scarred
# 2 scarred
# 3 happy
# 4 cheerful
# 5 mad
# 6 mad
# 7 mad
# 8 mad
# 9 mad
#10 curious
#11 curious
#12 curious
#13 curious
这将给出一列中的所有单词,可以用作词云的输入。
在 base R 中,另一种方式可能是:
names(word_tally)[which(word_tally != 0, arr.ind = TRUE)[,2]]
使用 rep
和 colSums
:
words <- rep(names(word_tally), colSums(word_tally))
words
[1] "scarred" "scarred" "happy" "cheerful" "mad"
[6] "mad" "mad" "mad" "mad" "curious"
[11] "curious" "curious" "curious"
或者因为频率是列总和,所以只使用数据。
wordcloud(names(word_tally), freq=colSums(word_tally), min.freq = 1)
这是我的计数的重复:
word_tally <- data.frame(scarred = c(1,1,0,0,0,0,0,0,0,0,0,0,0),
happy = c(0,0,1,0,0,0,0,0,0,0,0,0,0),
cheerful = c(0,0,0,1,0,0,0,0,0,0,0,0,0),
mad = c(0,0,0,0,1,1,1,1,1,0,0,0,0),
curious = c(0,0,0,0,0,0,0,0,0,1,1,1,1))
要制作词云,我似乎需要 1 个包含所有词的列。我怎样才能转换上述数据框来为词云制作这种类型的结构?
您可以获取长格式的数据并删除 value = 0
中的行。
library(dplyr)
tidyr::pivot_longer(word_tally, cols = everything(), names_to = "word") %>%
filter(value != 0) %>%
select(word)
# A tibble: 13 x 1
# word
# <chr>
# 1 scarred
# 2 scarred
# 3 happy
# 4 cheerful
# 5 mad
# 6 mad
# 7 mad
# 8 mad
# 9 mad
#10 curious
#11 curious
#12 curious
#13 curious
这将给出一列中的所有单词,可以用作词云的输入。
在 base R 中,另一种方式可能是:
names(word_tally)[which(word_tally != 0, arr.ind = TRUE)[,2]]
使用 rep
和 colSums
:
words <- rep(names(word_tally), colSums(word_tally))
words
[1] "scarred" "scarred" "happy" "cheerful" "mad"
[6] "mad" "mad" "mad" "mad" "curious"
[11] "curious" "curious" "curious"
或者因为频率是列总和,所以只使用数据。
wordcloud(names(word_tally), freq=colSums(word_tally), min.freq = 1)