How do I convert an R-frame of data with one column to a corpus for tm so that each row is treated as a document?
I wanted to use the command findAssocs
for a package tm
, but it only works when there are multiple documents in the corpus. Instead, I have a one column dataframe where each line contains text from a Tweet. Is it possible to convert this to a corpus that takes each line as a new document?
VCorpus (documents: 1, metadata (corpus/indexed): 0/0)
TermDocumentMatrix (terms: 71, documents: 1)
I have 10 rows of data that need to be converted as
VCorpus (documents: 10, metadata (corpus/indexed): 0/0)
TermDocumentMatrix (terms: 71, documents: 10)
+3
source to share
1 answer
I would recommend that you read tm
-vignette first before continuing. Answer your specific question below.
Create example data:
txt <- strsplit("I wanted to use the findAssocs of the tm package. but it works only when there are more than one documents in the corpus. I have a data frame table which has one column and each row has a tweet text. Is it possible to convert the into a corpus which takes each row as a new document?", split=" ")[[1]]
data <- data.frame(text=txt, stringsAsFactors=FALSE)
data[1:5, ]
Import your data to Source, your Source to Corpus, and then bring in TDM from your Corpus:
library(tm)
tdm <- TermDocumentMatrix(Corpus(DataframeSource(data)))
show(tdm)
#A term-document matrix (35 terms, 58 documents)
#
#Non-/sparse entries: 43/1987
#Sparsity : 98%
#Maximal term length: 10
#Weighting : term frequency (tf)
str(tdm)
#List of 6
# $ i : int [1:43] 32 31 28 12 28 21 3 35 20 33 ...
# $ j : int [1:43] 2 4 5 6 8 10 11 13 14 15 ...
# $ v : num [1:43] 1 1 1 1 1 1 1 1 1 1 ...
# $ nrow : int 35
# $ ncol : int 58
# $ dimnames:List of 2
# ..$ Terms: chr [1:35] "and" "are" "but" "column" ...
# ..$ Docs : chr [1:58] "1" "2" "3" "4" ...
# - attr(*, "class")= chr [1:2] "TermDocumentMatrix" "simple_triplet_matrix"
# - attr(*, "Weighting")= chr [1:2] "term frequency" "tf"
+4
source to share