使用R在一个.csv文件中写入不同的数据帧 [英] Write different data frame in one .csv file with R
问题描述
我有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" featureThe problem with
write.table
: csv files fromwrite.table
are not read prettily by Excel 2010 like those fromwrite.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屋!