[R] selectively aggregate data

Petr Pikal petr.pikal at precheza.cz
Tue Jul 16 16:49:46 CEST 2002



On 16 Jul 2002 at 15:06, alexander.schnee at tuebingen.mp wrote:

> I'm shure that this kind of question is asked not for the fist time,
> but i would be glad if someone of you could help me out I have a
> data.frame like this :
>     subjnr group block     data
> 1       33     1     1  564.476
> 2       33     1     1  289.185
> 3       33     1     1  483.372
> 4       33     1     1 1461.260
> 5       33     1     1  315.353
> 6       33     1     1  351.870
> 7       33     1     1 1238.350
> 8       33     1     1  218.135
> 9       33     1     1  335.486
> 10      33     1     1  312.848
> 11      33     1     1  224.989
> 12      33     1     1  383.283
> 13      33     1     1  317.788
> 14      33     1     1  310.263
> 15      33     1     1  216.295
> 16      33     1     1  309.118
> 17      33     1     1  216.223
> 33      35     1     1 1234.130
> 34      35     1     1  318.243
> 35      35     1     1  316.154
> 36      35     1     1 1478.610
> 37      35     1     1  528.299
> 38      35     1     1  253.566
> 39      35     1     1 1621.200
> 40      35     1     1  711.374
> 41      35     1     1  454.701
> 42      35     1     1  311.847
> 43      35     1     1  734.801
> 44      35     1     1  938.706
> 45      35     1     1  432.131
> 46      35     1     1 1498.540
> 47      35     1     1  330.199
> 48      35     1     1  694.136
> 49      35     1     1  321.608
> 65      37     1     1 1284.240
> .........
> and i like to have a data.frame with the values of the first 3
> colloumns preserved and in  colloumn 4 the mean  of each subjnr. it
> should look like this :
>     subjnr group block     data
> 1       33     1     1  444.017
> 2       35     1     1  716.367
> 3       37     1     1  428.507
> 
> I'm trying aggregate but i did not get the Parameters to fill in
> thanks a lot if you can help me

I do not know, if it is posible to do it in one step. Partly it depends 
on whether variables group and block changes throughout your 
data frame.

If not the syntax shall be:

aggregate(data,list(subjnr),mean,na.rm=T)

and than you manually add vectors for group and block. }see 
?cbind

If the values of groups and blocks changes (and you want means 
for them as well), it can be done in one step by extending list 
argument.

aggregate(data,list(subjnr, group, block),mean,na.rm=T)

BTW it is usefull to give an example of your code (even if it does 
not work correctly:-) as it can give more clear picture of what you 
exactly want.


> 
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.- r-help mailing list -- Read
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help",
> or "[un]subscribe" (in the "body", not the subject !)  To:
> r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._

Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list