I wrote a code to count the appearance of words in a data frame:
Items <- c('decid*','head', 'heads')
df1<-data.frame(Items)
words<- c('head', 'heads', 'decided', 'decides', 'top', 'undecided')
df_main<-data.frame(words)
item <- vector()
count <- vector()
for (i in 1:length(unique(Items))){
item[i] <- Items[i]
count[i]<- sum(df_main$words == item[i])}
word_freq <- data.frame(cbind(item, count))
word_freq
However, the results are like this:
item | count | |
---|---|---|
1 | decid* | 0 |
2 | head | 1 |
3 | heads | 1 |
As you see, it does not correctly count for "decid*". The actual results I expect should be like this:
item | count | |
---|---|---|
1 | decid* | 2 |
2 | head | 1 |
3 | heads | 1 |
I think I need to change the item word (decid*) format, however, I could not figure it out. Any help is much appreciated!