简体   繁体   中英

How to accumulate values in a column and add the new earned value in a new column in R?

I have a data file looks like:

151
24
234
100
200
10
100
23
1
 ...

I want to have a new column in which each value in it is accumulated with previous value. For example -151+0=151 , 24+151=175 , 234+175=409 so: output:

151
175
409
509
709
719
819
842
843
...

any suggestion please?

look at the TTR package.

    library(TTR)
    x=c(151,24,234,100,200,10,100,23,1)
    y=runSum(x,n=2,cumulative=TRUE)
    y
 x <- c(151,24,234,100,200,10,100,23,1)
cumsum(x)

output:: [1] 151 175 409 509 709 719 819 842 843

This gave me the output as you want. Might help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM