Reputation: 231
I'm not sure why I can't wrap my head around this, but maybe someone can help. I've done the following using a for loop:
v <- c(500,498,457,400,376,300,210,157,64,10,2,2,2,1,1,1,1,1,1,1)
partsum <- 0
total <- sum(v)
percov <- numeric()
for (i in 1:length(v)){
partsum <- partsum + v[i]
percov[i] <- 100 * partsum/total
}
percov
However, I am dealing with a massive amount of data, and the for loop is quite slow. I feel like there definitely has to be another way to do this in R, and it might be faster. Something like:
percov <- 100 * partsum/sum(v)
where partsum at each element's index is the sum of all elements of v that are greater than or equal to the value of v for that element. But how would I do that?
Upvotes: 4
Views: 4596
Reputation: 887991
We can divide the cumulative sum (cumsum
) of the vector
by the sum
of the vector
and multiply by 100
percovN <- 100*cumsum(v)/sum(v)
Checking the output from OP's method
identical(percovN, percov)
#[1] TRUE
Upvotes: 5