问题
In addition to question R Text mining - how to change texts in R data frame column into several columns with word frequencies? I am wondering how I can manage to make columns with bigrams frequencies instead of just word frequencies. Again, many thanks in advance!
This is the example data frame (thanks to Tyler Rinker).
person sex adult state code
1 sam m 0 Computer is fun. Not too fun. K1
2 greg m 0 No it's not, it's dumb. K2
3 teacher m 1 What should we do? K3
4 sam m 0 You liar, it stinks! K4
5 greg m 0 I am telling the truth! K5
6 sally f 0 How can we be certain? K6
7 greg m 0 There is no way. K7
8 sam m 0 I distrust you. K8
9 sally f 0 What are you talking about? K9
10 researcher f 1 Shall we move on? Good then. K10
11 greg m 0 I'm hungry. Let's eat. You already? K11
Data set above:
library(qdap); DATA
回答1:
The dev version of qdap
(should go to CRAN within the next few days) does ngrams. For now you'll need to use the dev version. On the toy data set this is fast but on a larger data set such as qdap
's mraja1
data set requires ~5 minutes to complete. You could:
- Select the bigrams more wisely (i.e., don't use them all as there's going to be a ton)
- Wait the time
- Run it in parallel
- Figure out another way to do this
- Get a faster computer
Here's the code to get the dev version of qdap
and run the bigram search:
library(devtools)
install_github("qdap", "trinker")
library(qdap)
## this gets the bigrams
bigrams <- sapply(ngrams(DATA$state)[[c("all_n", "n_2")]], paste, collapse=" ")
## This searches by grouping variable for bigram use
termco(DATA$state, DATA$person, bigrams)
## To get raw values
termco(DATA$state, DATA$person, bigrams)[["raw"]]
来源:https://stackoverflow.com/questions/16626168/r-text-mining-how-to-change-texts-in-r-data-frame-column-into-several-columns