Write list of data.frames to separate CSV files with lapply

前端 未结 3 1750
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 02:11

The question says it all - I want to take a list object full of data.frames and write each data.frame to a separate .csv file where the name of the .csv file corresponds to

相关标签:
3条回答
  • 2020-11-29 02:23

    You could use mapply:

    mapply(
      write.table,
      x=df.daily, file=paste(names(df.daily), "txt", sep="."),
      MoreArgs=list(row.names=FALSE, sep=",")
    )
    

    There is thread about similar problem on plyr mailing list.

    0 讨论(0)
  • 2020-11-29 02:27

    A couple of things:

    laply performs operations on a list. What you're looking for is d_ply. And you don't have to break it up by day, you can let plyr do that for you. Also, I would not use names(x) as that returns all of the column names of a data.frame.

    d_ply(df, .(theday), function(x) write.csv(x, file=paste(x$theday,".csv",sep=""),row.names=F))
    
    0 讨论(0)
  • 2020-11-29 02:34

    Try this:

    sapply(names(df.daily), 
     function (x) write.table(df.daily[[x]], file=paste(x, "txt", sep=".") )   )
    

    You should see the names ("1", "2", "3") spit out one by one, but the NULLs are the evidence that the side-effect of writing to disk files was done. (Edit: changed [] to [[]].)

    0 讨论(0)
提交回复
热议问题