Sequentially summing values in a column with value above

This is probably simple, but I cannot figure it out. In R, I would like to take adjacent values and add to the sum of the values in another column. In Excel it would be where the value is added to the cell value along with the value of the cell above except for very first value because there is no value above. Better explained is this table.

value value_summed
28 28
41 69
71 140
92 232
39 271
64 335
25 360
21 381
17 398
59 457
38 495
97 592

How do I do this?

Thanks,
Jeff

Is this what you want?

dat1 <- tibble::tribble(
  ~value, ~value_summed,
     28L,           28L,
     41L,           69L,
     71L,          140L,
     92L,          232L,
     39L,          271L,
     64L,          335L,
     25L,          360L,
     21L,          381L,
     17L,          398L,
     59L,          457L,
     38L,          495L,
     97L,          592L
  )

cumsum(dat1$value)

Yes. Using your suggested cumsum, here is how I did it.

value <- c(28, 41, 71, 92, 39, 64, 25, 21, 17, 59, 38, 97)
dat1 <- data.frame(value)
dat1$value_summed <- cumsum(dat1$value)
dat1
#>    value value_summed
#> 1     28           28
#> 2     41           69
#> 3     71          140
#> 4     92          232
#> 5     39          271
#> 6     64          335
#> 7     25          360
#> 8     21          381
#> 9     17          398
#> 10    59          457
#> 11    38          495
#> 12    97          592

Pretty simple. Glad I asked.

Thanks,
Jeff

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.