Skip to content
Prev 226958 / 398500 Next

Function on columns of a dataframe

On Jul 9, 2010, at 10:26 AM, Eik Vettorazzi wrote:

            
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?