【问题标题】:Exporting data in CSV format on the same table in different columns在不同列的同一张表上以 CSV 格式导出数据
【发布时间】:2019-02-05 16:05:00
【问题描述】:

我正在编写几个结果表并以 Excel 格式导出它们。这是我在 R 中的代码的快速示例:

prob = attributes(.Last.value)
clas1=factor(kn1)

#Exporting clas1 into Excel format. The file contains a single column and 430 rows

filename = paste("results", kk, ".csv", sep="")
write.csv(clas1, filename)

#Exporting prob into Excel format. The file also contains a single column and 430 rows 

fileprobs = paste("probs", kk, ".csv", sep="")
write.csv (prob$prob, fileprobs)

由于 prob 和 clas1 都已链接,我想将它们导出到一个包含两列和 430 行的表中,而不是将它们拆分为两个不同的文件。

有没有办法做到这一点并将"prob" 作为prob 列的标题和"class" 作为clas1 列的标题?我像这样使用colnames:

write.csv (prob$prob, fileprobs,col.names =c("prob"))

但我收到以下错误消息:

 Warning message:
In write.csv(clas1, filename, col.names = c("classes")) :
  attempt to set 'col.names' ignored

感谢任何输入。

【问题讨论】:

    标签: r export-to-csv


    【解决方案1】:

    如果您已经按照正确的顺序排列了两列,如

    prob <- 1:10
    clas1 <- letters[1:10]
    

    然后你可以把它们放在同一个数据框中并导出数据框:

    bothColumns <- data.frame(prob=prob, class=clas1)
    
    write.csv(x=bothColumns, file="both columns.csv", row.names=FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2020-04-10
      • 2022-11-29
      • 2016-06-05
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多