Reputation: 372
i have the following column
1
0
0
1
1
1
and i want a new column with the sum of the values row by row wo something like this
1 1
0 1
0 1
1 2
1 3
1 4
thanks
Upvotes: 2
Views: 79
Reputation: 61154
Use cumsum
> x <- c(1,0,0,1,1,1)
> x
[1] 1 0 0 1 1 1
> cumsum(x)
[1] 1 1 1 2 3 4
Putting altogether
> cbind(x, xsum=cumsum(x))
x xsum
[1,] 1 1
[2,] 0 1
[3,] 0 1
[4,] 1 2
[5,] 1 3
[6,] 1 4
Upvotes: 1