To find bag of words in R
library(qdap)
bag_of_words(x,apostrophe.remove) – To find the bag of words (x – text ,apostrophe.remove – To remove apostrophe)
Load required libraries
Load the data
Use the function bag_of_words() to find the bag of words
#Load necessary libaries
library(“readtext”)
library(qdap)
data data1=(strsplit(data$text,”\n”))
data2=unlist(data1[[1]])
data3=strsplit(data2,”\t”)
data4=unlist(data3)
i=0
j=0
k=0
text=c()
pol=c()
for (i in (1:length(data4)))
{
if(i%%2!=0)
{
j=j+1
text[j]=data4[i]
}else
{
k=k+1
pol[k]=data4[i]
}
}
df=data.frame(text=text,polarity=pol,stringsAsFactors = FALSE)
df$text[1:10]
#To find the bag of words
bag_o_words(df$text[1:10])
bag_o_words(df$text[1:10], apostrophe.remove = TRUE)