【发布时间】:2014-07-24 23:46:56
【问题描述】:
我的data.csv 文件包含以下内容:
id,name
143,The sky is blue.
21,The sun is bright.
23,The sun in the sky is bright.
现在,我可以像这样读取整个文件:
> file_loc <- "test.csv"
> x <- read.csv(file_loc, header = TRUE)
> x <- data.frame(lapply(x, as.character), stringsAsFactors=FALSE)
> require(tm)
Loading required package: tm
> dd <- Corpus(DataframeSource(x))
> dtm <- DocumentTermMatrix(dd, control = list(weighting = weightTfIdf))
我得到的结果矩阵是:
> as.matrix(dtm)
Terms
Docs 143 blue. bright. sky sun the
1 0.3962406 0.3962406 0.0000000 0.1462406 0.0000000 0
2 0.0000000 0.0000000 0.1949875 0.0000000 0.1949875 0
3 0.0000000 0.0000000 0.1169925 0.1169925 0.1169925 0
我想要的是将csv 文件的id 列作为docs 的名称,如下所示:
Terms
Docs blue. bright. sky sun the
143 0.3962406 0.0000000 0.1462406 0.0000000 0
21 0.0000000 0.1949875 0.0000000 0.1949875 0
23 0.0000000 0.1169925 0.1169925 0.1169925 0
谁能指导我如何达到预期的效果?
【问题讨论】:
-
也许将 id 读取为行名?将
rownames = 1添加到 read.csv
标签: r csv matrix machine-learning tf-idf