[R] Odp: R: ave and quantile
Petr PIKAL
petr.pikal at precheza.cz
Fri Nov 16 11:38:59 CET 2007
Hi
Patrick Hausmann <c18g at uni-bremen.de> napsal dne 15.11.2007 18:59:06:
> Hello Petr,
>
> one question solved, the next is standing in front of me... If you
> have a minute to look .... great!
>
> > x
> V1 V2 F1
> 1 A 2 0.1552277
> 2 A 3 0.1552277
> 3 A 4 0.1552277
> 4 B 3 0.8447723
> 5 B 2 0.8447723
> 6 C 6 0.2500000
> 7 C 2 0.2500000
> 8 C 6 0.2500000
>
> Getting the mean is no problem ;-)
> > ave(x$V2, x$V1, FUN = mean)
> [1] 3.000000 3.000000 3.000000 2.500000 2.500000 4.666667 4.666667
4.666667
>
> But I don't know, how to use the "quantile"-function with the "ave",
> using the values form x$F1? This doesn't work:
>
> ave(x$V2, x$V1, FUN = function(x) quantile(x, prob = x$F1))
A little of surgery.
> ave(x$V2, x$V1, FUN = function(x) quantile(x, prob = x$F1))
Error in x[i] <- value[[j]] : nothing to replace with
> ave(x$V2, x$V1, FUN = function(x) quantile(x, prob = .8))
[1] 3.6 3.6 3.6 2.8 2.8 6.0 6.0 6.0
> quantile(x$V2, prob = .8)
80%
5.2
> quantile(x$V2, prob = x$F1)
15.52277% 15.52277% 15.52277% 84.47723% 84.47723% 25% 25% 25%
2.000000 2.000000 2.000000 5.826812 5.826812 2.000000 2.000000
2.000000
function in ave needs to evaluate to 1 value and you just want to get
several values based on x$F1.
Here comes your turn, because you need to know what you want either a
quantile for one value or a quantiles for several values.
If later you can not use "ave" but you need to use "by", "tapply", or
"aggregate" and reformat the output according your intentions.
However you can do another construction and use indexed group averaging
like
> ave(seq_along(x$V2), x$V1, FUN = function(s) quantile(x$V2[s], prob =
x$F1[s]))
[1] 2.310455 2.310455 2.310455 2.844772 2.844772 4.000000 4.000000
4.000000
Regards
Petr
>
> Thank you for any hint!!
>
> Best
> Patrick
>
>
More information about the R-help
mailing list