Skip to content

by class...

7 messages · Onur Uncu, arun, David Winsemius +2 more

#
I used the by() function on a data.frame to get sums of the data grouped by 2 factors. The function worked however the output is in a class called 'by'. Not familiar with this class. How can I turn the output into a nice table where columns represent values of factor1, row represent values of factor2 and the entries in the table are the sums that were calculated using the by function?

I did some web search which suggested using do.call(rbind, datframe_object) but this command gave the following error:
"Second argument must be a list"...


Thank you.
#
On Dec 20, 2013, at 9:38 AM, Onur Uncu wrote:

            
The output of by() is always a list, so I would have expected:

do.call(rbind, by.object) to have retruned a different error message that what you suggest.
David Winsemius
Alameda, CA, USA
#
Hi,
Try:
as.table(by(warpbreaks[,1],warpbreaks[,-1],sum))


#or to convert to data.frame

as.data.frame(as.table(by(warpbreaks[,1],warpbreaks[,-1],mean)))
A.K.
On Friday, December 20, 2013 12:39 PM, Onur Uncu <onuruncu at gmail.com> wrote:
I used the by() function on a data.frame to get sums of the data grouped by 2 factors. The function worked however the output is in a class called 'by'. Not familiar with this class. How can I turn the output into a nice table where columns represent values of factor1, row represent values of factor2 and the entries in the table are the sums that were calculated using the by function?

I did some web search which suggested using do.call(rbind, datframe_object) but this command gave the following error:
"Second argument must be a list"...


Thank you.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Hi,
You can also try:
library(reshape2)
dcast(as.data.frame(as.table(by(warpbreaks[,1],warpbreaks[,-1],sum))),wool~tension, value.var="Freq")


A.K.
On , arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:
as.table(by(warpbreaks[,1],warpbreaks[,-1],sum))


#or to convert to data.frame

as.data.frame(as.table(by(warpbreaks[,1],warpbreaks[,-1],mean)))
A.K.
On Friday, December 20, 2013 12:39 PM, Onur Uncu <onuruncu at gmail.com> wrote:
I used the by() function on a data.frame to get sums of the data grouped by 2 factors. The function worked however the output is in a class called 'by'. Not familiar with this class. How can I turn the output into a nice table where columns represent values of factor1, row represent values of factor2 and the entries in the table are the sums that were calculated using the by function?

I did some web search which suggested using do.call(rbind, datframe_object) but this command gave the following error:
"Second argument must be a list"...


Thank you.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
On Dec 20, 2013, at 9:45 AM, David Winsemius wrote:

            
Or maybe my memories are false.

?by
"This is always a list if simplify is false, otherwise a list or array (see tapply)."

So maybe you need simplify=FALSE
David Winsemius
Alameda, CA, USA
#
You seem to be falling prey to a common misconception that "R" is some monolithic tool, when in fact it is a herd of cats.

The "by" function, from the "base" package, returns a list of results returned by your function. One approach to making a data frame out of that is to use the simplify2array function, transpose, and convert to data frame.

However, there are many other ways to aggregate data as well. Packages plyr, sqldf, data.table all have strengths and weaknesses, but you must always keep in mind that they are contributed packages and you have to refer to the package name if you ask here about using functions from them.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Onur Uncu <onuruncu at gmail.com> wrote:
#
Nominate for a fortune.

John Kane
Kingston ON Canada
You seem to be falling prey to a common misconception that "R" is some monolithic tool, when in fact it is a herd of cats.

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!