使用R在一个.csv文件中写入不同的数据帧 [英] Write different data frame in one .csv file with R

查看:745
本文介绍了使用R在一个.csv文件中写入不同的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个数据框,我希望他们被写在一个单一的.csv文件,一个在其他之上,而不是在同一个表。所以,在一个csv文件中有3个不同的表。他们都有相同的大小。



write.csv 的问题:它不包含 append



write.table 的问题: write.table 不会被Excel 2010读取,例如 write.csv







帖子我已经阅读,我找不到解决我的问题:





解决方案?

解决方案

write.csv 只需调用 write.table 所以你可以通过3次调用 write.table 来实现你想要的。

  write.table(df1,filename.csv,col.names = TRUE,sep =,)
write.table(df2,filename.csv,col.names = FALSE,sep = ,,append = TRUE)
write.table(df3,filename.csv,col.names = FALSE,sep =,,append = TRUE)



实际上,你可以通过将你的数据帧与rbind组合成一个df,然后调用write.csv一次来避免整个问题。

  write.csv(rbind(df1,d32,df3),filename.csv)


I have 3 data frame, and I want them to be written in one single .csv file, one above the others, not in a same table. So, 3 different tables in one csv file. They all have same size.

The problem with write.csv: it does not contain "append" feature

The problem with write.table: csv files from write.table are not read prettily by Excel 2010 like those from write.csv

Post I already read and in which I could not find solution to my problem :

Solution ?

解决方案

write.csv just calls write.table under the hood, with appropriate arguments. So you can achieve what you want with 3 calls to write.table.

write.table(df1, "filename.csv", col.names=TRUE, sep=",")
write.table(df2, "filename.csv", col.names=FALSE, sep=",", append=TRUE)
write.table(df3, "filename.csv", col.names=FALSE, sep=",", append=TRUE)

Actually, you could avoid the whole issue by combining your data frames into a single df with rbind, then calling write.csv once.

write.csv(rbind(df1, d32, df3), "filename.csv")

这篇关于使用R在一个.csv文件中写入不同的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆