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

Robert Hijmans r.hijmans at gmail.com
Fri Mar 13 10:16:53 CET 2009


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