[R] Sparseby Problems

William Dunlap wdunlap at tibco.com
Wed Apr 21 22:48:30 CEST 2010


sparseby's FUN argument needs to be a function
that accepts a data.frame.

mean() has a data.frame method that returns a vector
of column means but most of the other summary functions
(e.g., median, min, max, quantile) do not work on data.frames.

You can use aggregate() instead of by() or sparseby(), as
its function is applied to each column of each data.frame,
not to a whole data.frame.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Stewart
> Sent: Wednesday, April 21, 2010 10:17 AM
> To: r-help at r-project.org
> Subject: [R] Sparseby Problems
> 
> I've got a problem with the sparseby command (reshape 
> library), and I have
> reached the peak of my R knowledge (it isn't really that high).
> 
> I have a small data frame of 23 rows and 15 columns, here is 
> a subset, the
> first four columns are factors and the rest are numeric (only 
> one, line54 is
> provided).
> 
>    bearID YEAR Season SEX      line54
> 5    1900    8      3   0  16.3923519
> 11   2270    5      1   0 233.7414014
> 12   2271    5      1   0 290.8207652
> 13   2271    5      2   0 244.7820844
> 15   2291    5      1   0   0.0000000
> 16   2291    5      2   0  14.5037795
> 17   2291    6      1   0   0.0000000
> 18   2293    5      2   0 144.7440752
> 19   2293    5      3   0   0.0000000
> 20   2293    6      1   0  16.0592270
> 21   2293    6      2   0  30.1383426
> 28   2298    5      1   0   0.9741067
> 29   2298    5      2   0   9.6641018
> 30   2298    6      2   0   8.6533828
> 31   2309    5      2   0  85.9781303
> 32   2325    6      1   0 110.8892153
> 35   2331    6      1   0  26.7335562
> 44   2390    7      2   0   7.1690620
> 45   2390    8      2   0  44.1109897
> 46   2390    8      3   0 503.9074898
> 47   2390    9      2   0   8.4393660
> 54   2416    7      3   0  48.6910907
> 58   2418    8      2   0   5.7951139
> 
> Sparseby works fine when I try to calculate mean
> 
> >sparseby(mF[1:5], mF$Season, mean)
> 
>   mF$Season bearID YEAR Season SEX    line54
> 1         1     NA   NA     NA   0  84.90228
> 2         2     NA   NA     NA   0  54.90713
> 3         3     NA   NA     NA   0 142.24773
> 
> But it goes nuts when looking for max or min
> 
> > sparseby(mF[5:6], mF$Season, max)
>   mF$Season structure(c(2169.49621795108, 1885.22677689026, 
> 2492.17544685464
> 1         1                                                   
>       2169.496
> 2         2                                                   
>       1885.227
> 3         3                                                   
>       2492.175
> 
> Any ideas? All I want is to calculate create three 
> data.frames, mean, min
> and max.
> 
> Thanks,
> 
> Ben Stewart
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list