Is it possible to use write.table() and ddply, together?

后端 未结 2 1030
[愿得一人]
[愿得一人] 2020-12-10 08:53

Let\'s say I have a data.frame like:

a <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10)
df <- data.frame(a,rnorm(100))

And I

相关标签:
2条回答
  • 2020-12-10 09:23

    It's possible to do it with ddply, but that is not what the function was designed for. From the plyr documentation:

    All plyr functions use the same split-apply-combine strategy...

    You want to split the data.frame and apply a function but you don't want to return anything, so ddply will throw an error if you don't return something that can be combined into a data.frame.

    0 讨论(0)
  • 2020-12-10 09:39

    Continuing from Joshua's answer, the plyr function to use is d_ply which does not expect to return anything. You can do something like this:

    d_ply(df, .(a),
          function(sdf) write.csv(sdf,
                                  file=paste(sdf$a[[1]],".csv",sep="")))
    

    The file argument to write.csv is constructed such that each subset gets a different filename.

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