[R] different interface to by (tapply)?

David Winsemius dwinsemius at comcast.net
Mon Aug 30 17:02:44 CEST 2010


On Aug 30, 2010, at 9:19 AM, ivo welch wrote:

> dear R experts:
>
> has someone written a function that returns the results of by() as a
> data frame?   of course, this can work only if the output of the
> function that is an argument to by() is a numerical vector.
> presumably, what is now names(byobject) would become a column in the
> data frame, and the by object's list elements would become columns.
> it's a little bit like flattening the by() output object (so that the
> name of the list item and its contents become the same row), and
> having the right names for the columns.  I don't know how to do this
> quickly in the R way.  (Doing it slowly, e.g., with a for loop over
> the list of vectors, is easy, but would not make a nice function for
> me to use often.)
>
> for example, lets say my by() output is currently
>
> by( indf, indf$charid, function(x) c(m=mean(x), s=sd(x)) )
>
> $`A`
> [1] 2 3
> $`B`
> [2] 4 5

Doesn't by() return names for the "m" and "s" values?

>
> then the revised by() would instead produce
>
> charid  m  s
> A          2  3
> B          4  5

by shares with table and tapply the return of a list of matrix or  
array-like objects.

I would expect that as.data.frame would return a dataframe from such  
an argument, but my experience is that it generally forces this into a  
"long" format and one may need to resort to reshape() or reshape::melt  
and reshape::cast() to get it back into a rectangular format. You  
_could_ have created a richer example that would become the basis for  
further elaboration.

-- 
David.


>
> working with data frames is often more intuitive than working with the
> output of by().  the R wizards are probably chuckling now about how
> easy this is...
>
> regards,
>
> /iaw
>
> ----
> Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.com)
>
> ______________________________________________
> 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, MD
West Hartford, CT



More information about the R-help mailing list