How to append a whole dataframe to a CSV in R

前端 未结 3 1246
谎友^
谎友^ 2020-11-29 10:35

I can already append a row to a CSV using cat which makes that very easy:

  cat(\"my row, 1, 2, 3, 4\", \"mydf.csv\",sep=\"\\n\", append=TRU         


        
3条回答
  •  悲哀的现实
    2020-11-29 10:54

    If you have enough memory, the most efficient way is to bind them all together then write to file:

    df <- cbind(df1, df2, df3)
    write.csv(df, "spam.csv")
    

    Otherwise, you can use the append argument in write.csv:

    dfs <- c(df1, df2, df3)
    for (df in dfs){
      write.csv(df, "eggs.csv", append = TRUE)
    }
    

提交回复
热议问题