[R] Function on columns of a dataframe
Eik Vettorazzi
E.Vettorazzi at uke.uni-hamburg.de
Fri Jul 9 17:06:11 CEST 2010
just to satisfy my curiousity,
aggregate(bla, list(bla$cat), max)
works for me and resulted in
Group.1 x cat v1 v2 v3 v4
1 cat1 5 cat1 0.6337076 0.2887081 0.3629962 0.5328683
2 cat2 10 cat2 0.5519426 0.6076447 0.4593770 0.9632341
3 cat3 11 cat3 0.6094089 0.6152059 0.5670835 0.9084917
4 cat4 8 cat4 0.4772603 0.2149017 0.4534723 0.7824375
5 cat5 9 cat5 0.6582466 0.3150096 0.5512863 1.3524582
6 cat6 12 cat6 0.4632893 0.4498425 0.3926193 0.8023014
so, what didn't work for you, except for the "extra" columns? (taking
for granted, that random numbers aren't the same)
Am 09.07.2010 16:47, schrieb David Winsemius:
>
> On Jul 9, 2010, at 10:26 AM, Eik Vettorazzi wrote:
>
>> you are right. But maybe "aggregate" is close to the desired result?
>>
>> aggregate(bla, list(bla$cat), max)
>
> Right. I couldn't get it to work until I removed the first two columns:
>
> aggregate(bla[,-(1:2)], list(bla$cat), max)
>
> Then I got pretty much the same dataframe as I would have with :
>
> as.data.frame(lapply( bla[, -(1:2)], function(x) tapply(x, bla$cat,
> max) ))
> v1 v2 v3 v4
> cat1 0.4634519 0.5274645 0.6051479 0.7586322
> cat2 0.4062700 0.4282639 0.4443707 0.8419526
> cat3 0.4816403 0.4996033 0.3538144 0.9456385
> cat4 0.6354560 0.3558259 0.3646292 0.1907295
> cat5 0.6663811 0.2154201 0.5059900 0.7573575
> cat6 0.5260832 0.3934063 0.3545962 0.6412563
>
> Except that aggregate version returns it with a "Group.1" column of
> "cat"s while the other version returned it with the "cat" names in the
> rownames. A matter of taste?
>
--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
More information about the R-help
mailing list