Assume I have a vector containing the following values:
foo <- c(1:5)
[1] 1 2 3 4 5
Is there a function or otherwise quick way to get each value of foo
to add recursively to the numbers before it?
Desired Result is a vector containing these added values:
1 3 6 10 15
foo[1]
is1
,foo[1] + foo[2]
is1 + 2
=3
,foo[1] + foo[2] + foo[3]
is1 + 2 + 3
=6
, etc.
My attempt:
I came up with:
vapply(1:length(foo), function(x) sum(foo[1]:foo[x]),integer(1))
But I'm hoping there is a simpler way to do this...