[R] tapply output as a dataframe

David Winsemius dwinsemius at comcast.net
Thu Feb 3 18:50:16 CET 2011


On Feb 3, 2011, at 11:29 AM, Graves, Gregory wrote:

> On Mon, Apr 13, 2009 at 12:41 PM, Dan Dube <ddube-at-advisen.com>  
> wrote:
>
That is pushing two years ago, so I doubt very many people still have  
that posting on their mail-clients. (When I did go to the archives Dan  
Dube's problem was posed as how to bind "a":

dt = data.frame(bucket=rep(1:4,25),val=rnorm(100))
fn = function(x) { ret = c(unname(quantile(x,probs=seq(. 
25,.75,.25),na.rm=T)),mean(x,na.rm=T)) }
a = tapply(dt$val,dt$bucket,fn)


>> i use tapply and by often, but i always end up banging my head  
>> against
>> the wall with the output.
>
> The proposed solution of Dan's problem posted on R-help was:
>
>> do.call(rbind,a)
>
> When I use this 'solution' I get 'ERROR:  second argument must be a  
> list'.  So head on wall continues.
>
> My tapply output is generated as follows:
>
>> a=tapply(value,list(sampling.date,station.code),mean)

Why not give us sampling.date (which is probably NOT really a date but  
rather a character vector) and station.code so we can show you how to  
create a more appropriate structure?

>
> which gives me this (in part):
>
>             A     B     C     D     E     F     G     H     I      
> J      K
> 1/15/2008  0.004 0.027 0.019 0.015 0.035 0.022 0.007 0.038 0.042  
> 0.045 0.0350
> 1/15/2009  0.027 0.027 0.031 0.015 0.008 0.021 0.007 0.027 0.026  
> 0.029 0.0210
> 1/15/2010  0.016 0.020 0.015 0.022 0.015 0.013 0.007 0.014 0.019  
> 0.019 0.0180
> 10/15/2007 0.052 0.051 0.032 0.024 0.017 0.044 0.015 0.058 0.063  
> 0.061 0.0640
> 10/15/2008 0.042 0.054 0.030 0.017 0.024 0.030 0.019 0.044 0.047  
> 0.051 0.0390
> 10/15/2009 0.047 0.035 0.031 0.020 0.012 0.039 0.019 0.051 0.055  
> 0.054 0.0350

> The only way I can figure out how to resolve this, such that I can,  
> for example, plot station "A" against date, is to export the tapply  
> output as a csv, and then reimport.

>
> Suggestions?  I couldn't find a solution to this likely SIMPLE problem

Perhaps. but we haven't really been told what the problem is, have we?

> in Crawley or multiple searches of R help.

>
> Gregory A. Graves, Lead Scientist


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list