【问题标题】:From csv to txt从 csv 到 txt
【发布时间】:2013-11-12 09:32:58
【问题描述】:

如何将 csv 文件转换为纯文本文件?

我的 CSV 文件包含 3 列,我只想获取文本文件中“文本”列的值。我试图通过以下方式实现这一目标:

name <- read.csv('c:/Users/bi2/Documents/TextminingRfiles/ScoreOutput/RangersScores.csv', header=T, sep=",")
attach(name)
posText <- name[score > 0,]> name <- read.csv('c:/Users/bi2/Documents/TextminingRfiles/ScoreOutput/RangersScores.csv', header=T, sep=",")
attach(name)
posText <- name[score > 0,]
write(posText$text, file = "C:/Users/bi2/Documents/TextminingRfiles/ScoreOutput/namePositive.txt", sep="")

此代码仅将索引复制到文本文件,而不是文本列的文本值。我该如何解决这个问题?

Tnx 为您提供帮助。

【问题讨论】:

  • posText$text 的内容是什么?那应该给你一个提示

标签: r csv text


【解决方案1】:

write.table 的一些额外参数可能会得到你想要的。

这是一个可重现的示例,制作一个包含三列的 CSV...

write.csv(data.frame(x = sample(26),
                     y = sample(26),
                     text = letters),
          file = "test.csv")

现在将其读入R...

test <- read.csv("test.csv")

现在进行其他计算,然后进行子集化以仅获取要写入 txt 文件的列...

test <- test[ ,which(names(test) == 'text')]

现在将其写入一个 txt 文件,没有行名、列名或引号......

write.table(test, "test.txt", 
            row.names = FALSE, 
            quote = FALSE, 
            col.names = FALSE)

顺便说一句,您问题代码中的attaching 是不必要的,不推荐使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2021-08-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多