0

我想知道是否可以在不使用包 tm 的情况下构建 TermdocumentMatrix。

我正在考虑将两个 for 循环与 grep 结合使用,但不幸的是我没有设法创建有用的东西。

    matrix <- matrix(, nrow=length(lvector), ncol=length(lvector))



for(i in 1:length(lvector))
{
  for(j in 1:length(l))
  {
    lijst <- grep(lvector[i],l[j])
    if (length(lijst)==0)
    {
      matrix[i,j] == 0
    }
    else 
    {
      matrix[i,j] == 1
    }

  }
}

提前谢谢

4

1 回答 1

0

FWIW,这是一种方法:

get.dtm <- function(txts) {
  require(plyr)
  dtm <- do.call(rbind.fill.matrix, lapply(txts, function(txt) t(table(scan(file = textConnection(txt), what = "character", quiet = TRUE)))))
  dtm[is.na(dtm)] <- 0
  return(dtm)
}
get.dtm(c("this is a text text", "this is just another text"))
#      a is text this another just
# [1,] 1  1    2    1       0    0
# [2,] 0  1    1    1       1    1
于 2015-03-16T23:06:05.717 回答