[R-sig-Geo] Help to count in apply(...)

hadley wickham h.wickham at gmail.com
Fri Mar 13 14:02:58 CET 2009


On Fri, Mar 13, 2009 at 7:55 AM, Robert Hijmans <r.hijmans at gmail.com> wrote:
> I think the solution is using 'merge'
>
> soil.fe <- data.frame(
>  ID =c(1,1,1,1,2,2,3,3,4,5,5,5,5,5,9,9,9,9,9,9,10,10),
>  K =c(6.25,12.50,25.00,50.00,80.00,12.00,6.25,12.50,25.00,50.00,76.00,67.00,67.00,56.00,98.00,32.00,33.00,43.00,54.00,76.00,85.00,67.00))
>
>  fer.conc.max <- tapply(soil.fe$K, soil.fe$ID, max, na.rm=TRUE)
>  fm <- matrix(as.numeric(cbind(names(fer.conc.max), fer.conc.max)), ncol=2)
>  colnames(fm) <- c('ID', 'maxFe')
>  merge(soil.fe, fm)

Another alternative is to use ddply from the plyr package:

ddply(soil.fe, .(ID), transform, maxFe = max(K, na.rm = T))

You can find out more at http://had.co.nz/plyr.

Hadley


-- 
http://had.co.nz/



More information about the R-sig-Geo mailing list