Frequency of occurrence of two-pair combinations i

2019-08-27 07:45发布

I have a file with several string (text) variables where each respondent has written a sentence or two for each variable. I want to be able to find the frequency of each combination of words (i.e. how often "capability" occurs with "performance"). My code so far goes:

#Setting up the data file 
data.text <- scan("C:/temp/tester.csv", what="char", sep="\n")

#Change everything to lower text
data.text <- tolower(data.text)

#Split the strings into separate words
data.words.list <- strsplit(data.text, "\\W+", perl=TRUE)
data.words.vector <- unlist(data.words.list)

#List each word and frequency
data.freq.list <- table(data.words.vector)

This gives me a list of each word and how often it appears in the string variables. Now I want to see the frequency of every 2 word combination. Is this possible?

Thanks!

An example of the string data:

ID   Reason_for_Dissatisfaction    Reason_for_Likelihood_to_Switch
1    "not happy with the service"  "better value at other place"
2    "poor customer service"       "tired of same old thing"
3    "they are overchanging me"    "bad service"

1条回答
家丑人穷心不美
2楼-- · 2019-08-27 07:54

I'm not sure if this is what yu mean, but rather than splitting on every two word boundaires (which I found a pain to try and regex) you could paste every two words together using the trusty head and tails slip trick...

#  How I read your data
df <- read.table( text = 'ID   Reason_for_Dissatisfaction    Reason_for_Likelihood_to_Switch
1    "not happy with the service"  "better value at other place"
2    "poor customer service"       "tired of same old thing"
3    "they are overchanging me"    "bad service"
' , h = TRUE , stringsAsFactors = FALSE )


#  Split to words
wlist <- sapply( df[,-1] , strsplit , split = "\\W+", perl=TRUE)

#  Paste word pairs together
outl <- sapply( wlist , function(x) paste( head(x,-1) , tail(x,-1) , sep = " ") )

#  Table as per usual
table(unlist( outl ) )
are overchanging         at other      bad service     better value customer service 
               1                1                1                1                1 
      happy with        not happy          of same        old thing      other place 
               1                1                1                1                1 
 overchanging me    poor customer         same old      the service         they are 
               1                1                1                1                1 
        tired of         value at         with the 
               1                1                1
查看更多
登录 后发表回答