Adding numbers within a vector in r

微笑、不失礼 提交于 2019-12-04 04:57:48

问题


I have a vector

v<-c(1,2,3)

I need add the numbers in the vector in the following fashion

1,1+2,1+2+3

producing a second vector

v1<-c(1,3,6)

This is probably quite simple...but I am a bit stuck.


回答1:


Use the cumulative sum function:

cumsum(v)
#[1] 1 3 6


来源:https://stackoverflow.com/questions/12114127/adding-numbers-within-a-vector-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!