将带有一列的数据框转换为txt文档(每行一个txt文档)

时间:2016-07-19 07:12:50

标签: r tm

我需要将一列数据框转换为txt文档(每行一个txt文档),以便我可以使用需要语料库的tm包。我尝试使用strsplit函数,但我尝试过的都没有用。以下是一些示例数据:

Descriptions_of_Procedure (column name)
Anesthesia for lower leg cast application
Anesthesia for face
Anesthesia for upper leg
Dressing change

2 个答案:

答案 0 :(得分:1)

df<-data.frame(Descriptions_of_Procedure =c(
                  "Anesthesia for lower leg cast application",
                  "Anesthesia for face",
                  "Anesthesia for upper leg",
                  "Dressing change"))



for(i in 1:nrow(df)){
  write.csv(df[i,],paste0("line",i,".txt"),row.names=FALSE)
}

这将按行创建一个txt文件,其中df数据框只有一行

编辑:禁止文件

中列的名称

使用write.table()代替write.csv(),您可以使用col.names = FALSE提供该功能,然后只在文档row i中写入line i

for(i in 1:nrow(df)){
  write.table(df[i,],paste0("line",i,".txt"),row.names=FALSE,col.names = FALSE)
}

答案 1 :(得分:0)

DataframeSource()包的tm功能是为此而设计的。

library(tm)
# code from here
# example(DataframeSource)
docs <- data.frame(c("This is a text.", "This another one."))
(ds <- DataframeSource(docs))
inspect(VCorpus(ds))

(VCorpus中的V是可选的)

相关问题