Did you mean cumsum()?
> a <- c(4,4,5,1,9)
> a <- a[order(-a[])]
> k <- a[1:length(a)]/sum(a)
> k
[1] 0.391304 0.217391 0.173913 0.173913 0.043478
> k <- a/sum(a)
> k
[1] 0.391304 0.217391 0.173913 0.173913 0.043478
> ck <- cumsum(k)
> ck
[1] 0.39130 0.60870 0.78261 0.95652 1.00000
>
Edit I missed another simplification:
> a <- c(4,4,5,1,9)
> ck <- cumsum( sort(a, decr=TRUE) / sum(as) )
> ck
[1] 0.39130 0.60870 0.78261 0.95652 1.00000
>
You want it sort()here, not order()in combination with indexing.
source
share