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:> 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"...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.> > > 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.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.
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:>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.