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

Robert Hijmans r.hijmans at gmail.com
Fri Mar 13 13:55:27 CET 2009


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)

 R

On Fri, Mar 13, 2009 at 8:29 PM, gianni lavaredo
<gianni.lavaredo at gmail.com> wrote:
> Thanks Robert,
>
> I have a data frame with difefferent length of ID and I change the codes in
> this mode
>
> ID <- rep(1:10, each=10)
> K <- runif(100)
> areaFe <- as.data.frame(cbind(ID, K))
> fer.conc.max <- tapply(areaFe$K, areaFe$ID, max, na.rm=TRUE)
> # I am sure that all my value are under 100
> tapply(areaFe$K <= 100, areaFe$ID, sum, na.rm=TRUE)
>
> 1  2  3  4  5  6  7  8  9 10
> 10 10 10 10 10 10 10 10 10 10
>
> It's not elegant mode, and I ask please if there an elegant mode to write
> below (use "fer.conc.max")
>
>
> 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)
> #ee max value
> fer.conc.max
>
> 1    2    3    4    5    9   10
> 50.0 80.0 12.5 25.0 76.0 98.0 85.0
>
> # I am sure that all my value are under 100
> tapply(soil.fe$K <= 100, soil.fe$ID, sum, na.rm=TRUE)
>
> 1  2  3  4  5  9 10
>  4  2  2  1  5  6  2
>
>
> Thanks
>
> 2009/3/13 Robert Hijmans <r.hijmans at gmail.com>
>>
>> I see, I did not understand that. This is a quick fix, please check if
>> it is OK. Using which.max might be more elegant.
>>
>> ID <- rep(1:10, each=10)
>> K <- runif(100)
>> areaFe <- as.data.frame(cbind(ID, K))
>> fer.conc.max <- tapply(areaFe$K, areaFe$ID, max, na.rm=TRUE)
>> fermax <- rep (fer.conc.max, each=10)
>> tapply(areaFe$K > 0.9 * fermax, areaFe$ID, sum, na.rm=TRUE)
>>
>> Robert
>>
>> On Fri, Mar 13, 2009 at 5:02 PM, gianni lavaredo
>> <gianni.lavaredo at gmail.com> wrote:
>> > Thanks Robert
>> >
>> > the problem (on your example) this I need
>> >
>> > ID <- rep(1:10, each=10)
>> > K <- runif(100)
>> > areaFe <- as.data.frame(cbind(ID, K))
>> > #find the max value in each ID area
>> >> fer.conc.max <- tapply(areaFe$K, areaFe$ID, max, na.rm=TRUE)
>> >> fer.conc.max
>> >         1         2         3         4         5         6
>> > 7         8
>> > 0.9339687 0.9413441 0.9605550 0.9968569 0.9647085 0.9563837 0.9731356
>> > 0.9278238
>> >         9        10
>> > 0.9496833 0.9111127
>> > #count the value below or equal the fe.comc.max
>> >> tapply(areaFe$K <= fer.conc.max, areaFe$ID, sum, na.rm=TRUE)
>> > Error tapply(areaFe$K <= fer.conc.max, areaFe$ID, sum, na.rm = TRUE) :
>> >   dims [product 10] do not match the length of object [100]
>> >
>> >
>> >
>> > 2009/3/13 Robert Hijmans <r.hijmans at gmail.com>
>> >>
>> >> Dear Gianni,
>> >>
>> >> I think you want this:
>> >>
>> >> tapply(areaFe$K <= fer.conc.max, areaFe$ID, sum, na.rm=TRUE)
>> >>
>> >> E.g.:
>> >>
>> >> ID <- rep(1:10, each=10)
>> >> K <- runif(100)
>> >> areaFe <- as.data.frame(cbind(ID, K))
>> >> fer.conc.max <- 0.5
>> >> tapply(areaFe$K <= fer.conc.max, areaFe$ID, sum, na.rm=TRUE)
>> >>
>> >>
>> >> Robert
>> >>
>> >>
>> >> On Fri, Mar 13, 2009 at 9:58 AM, gianni lavaredo
>> >> <gianni.lavaredo at gmail.com> wrote:
>> >> > dear User
>> >> >
>> >> > I'm quite new to R and trying to learn the basic and sorry for the
>> >> > email. I
>> >> > don't wish abuse of mail-list. I had read manual and Help online
>> >> > about
>> >> > tapply (...). I have a data frame and I need "count" the Fe value
>> >> > with
>> >> > sum
>> >> > for every ID sample area
>> >> >
>> >> > fer.conc.max <- tapply(areaFe$K, areaFe$ID, max, na.rm=TRUE)
>> >> >
>> >> > I need to count the Fe area with Fe$K <=  fer.conc.max
>> >> >
>> >> > # give me the summatory and not the count
>> >> > fer.conc.max <- tapply(areaFe$K, areaFe$ID, sum, na.rm=TRUE)
>> >> >
>> >> > I tried sever combination that I found in mail-list archive but alway
>> >> > I
>> >> > have
>> >> > a error message
>> >> >
>> >> > thanks Gianni
>> >> >
>> >> >        [[alternative HTML version deleted]]
>> >> >
>> >> > _______________________________________________
>> >> > R-sig-Geo mailing list
>> >> > R-sig-Geo at stat.math.ethz.ch
>> >> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >> >
>> >
>> >
>
>



More information about the R-sig-Geo mailing list