tags:

views:

94

answers:

2

I think this is very easy but my R kung-fu is weak. I'm trying to create a vector of itself in a cumulative way. This code works but I'd like something much more elegant and automated. I have millions of rows that need to be cumulated.

a <- c(4,4,5,1,9)
a <- a[order(-a[])]
k <- a[1:length(a)]/sum(a)
w <- c(k[1],k[1]+k[2],k[1]+k[2]+k[3],k[1]+k[2]+k[3]+k[4],k[1]+k[2]+k[3]+k[4]+k[5])
w
A: 

Bing is my friend...I found the cumsum() function.

datayoda
If bing is your fried, then [rseek.org](http://rseek.org) is bound to be your uncle...
Dirk Eddelbuettel
great! thanks for the link
datayoda
+3  A: 

Did you mean cumsum() ?

> a <- c(4,4,5,1,9)
> a <- a[order(-a[])]            # just calling sort is shorter too
> k <- a[1:length(a)]/sum(a)     # long way  
> k 
[1] 0.391304 0.217391 0.173913 0.173913 0.043478
> k <- a/sum(a)                  # same, but shorter
> 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 overlooked 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 sort() here rather than order() coupled with indexing.

Dirk Eddelbuettel
thanks that even beter
datayoda
Or `prop.table(cumsum(sort(a, decr=TRUE)))`
hadley