0

I am using a by() function in R to run A FUN() on all subsets of a factor. The FUN() returns two numbers as its returning values. The by() function returns a "by" class like this:

--------------------------------------------------------------------------------------- 
: 98012
[1] 25.00  0.84
--------------------------------------------------------------------------------------- 
: 98301
[1] 5.0 0.6
--------------------------------------------------------------------------------------- 

I'd like to convert this "by" class into an ordinary data.frame so that I can use two returned numbers separately. However, such "by" class cannot be converted into a data.frame. Any ideas?

Chase
  • 67,710
  • 18
  • 144
  • 161
Leo5188
  • 1,967
  • 2
  • 17
  • 21

2 Answers2

3

You can do:

df <- do.call(cbind.data.frame, yourByResult)

Notice that you need to specify the cbind.data.frame otherwise, by using the simple cbind the result will be a matrix instead of a data.frame.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • digEmAll, Thanks for your suggestions. I found another question [link](http://stackoverflow.com/questions/1407449/for-each-group-summarise-means-for-all-variables-in-dataframe-ddply-split) and it gives in-detailed instructions to use do.call() – Leo5188 Jan 26 '12 at 21:23
2

I'd say just don't use by!

Instead use ddply from the plyr package with the syntax

ddply(data.frame,splitting-variable,f())

the key is that your function needs to take a data.frame and return a data.frame.

Justin
  • 42,475
  • 9
  • 93
  • 111