Reputation: 2103
Here is the repex of a tally I have:
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))
To make a word cloud seems I need 1 column with all the words. How could I transform the above dataframe to make that type of structure for a word cloud?
Upvotes: 0
Views: 100
Reputation: 389047
You could get the data in long format and remove rows where 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
This would give all the words in one column which can be used as input for wordcloud.
In base R, another way could be :
names(word_tally)[which(word_tally != 0, arr.ind = TRUE)[,2]]
Upvotes: 1
Reputation: 18823
Using rep
and 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"
Or since the frequencies are the column sums, using just the data.
wordcloud(names(word_tally), freq=colSums(word_tally), min.freq = 1)
Upvotes: 1