R:逐行汇总列的值并创建新列

[英]R:Summing up values of a column row by row and create new column


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

1 个解决方案

#1


1  

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/11/26/20a46f8263b1f9bc5e1544a44e6d3ddd.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号