使用R在一个.csv文件中写入不同的数据帧

Dav*_*vid 7 csv r file append dataframe

我有3个数据框,我希望它们写在一个.csv文件中,一个在其他文件之上,而不是在同一个表中.因此,一个csv文件中有3个不同的表.它们都有相同的尺寸.

问题write.csv:它不包含" 追加 "功能

问题write.table:csv文件来自write.tableExcel 2010,不像那些来自write.csv

这是<code> write.csv </ code>

这是<code> write.table </ code>

发布我已经阅读过了,我找不到解决问题的方法:

方案?

Hon*_*Ooi 18

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)
Run Code Online (Sandbox Code Playgroud)

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

write.csv(rbind(df1, d32, df3), "filename.csv")
Run Code Online (Sandbox Code Playgroud)


Dee*_*ena 11

我们使用sink文件:

# Sample dataframes:
df1 = iris[1:5, ]
df2 = iris[20:30, ]

# Start a sink file with a CSV extension
sink('multiple_df_export.csv')

 # Write the first dataframe, with a title and final line separator 
cat('This is the first dataframe')
write.csv(df1)
cat('____________________________')

cat('\n')
cat('\n')

# Write the 2nd dataframe to the same sink
cat('This is the second dataframe')
write.csv(df2)
cat('____________________________')

# Close the sink
sink()
Run Code Online (Sandbox Code Playgroud)