Cathyt10 Cathyt10 - 4 months ago 9
R Question

In R; I would like to do something in R rather than excel because excel can't handle the calculation. In excel the calculation is: =A2+SUM($B$2:B2)

I want col c

phys_pos
to be the value in col a
position
plus the accumulative value of col b
length
. In excel the calculation is:
=A2+SUM($B$2:B2)
, but excel can't handle such a lot of data. Thanks all.

The data I would like:

position length phys_pos
12 45 57
97 0 142
135 0 180
498 0 543
512 0 557
16 67 128
76 0 188
89 0 201
101 0 213
152 0 264
3 103 218
19 0 234
76 0 291
88 0 303

Answer

Look into dplyr https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html

install.packages("dplyr")

library(dplyr)

df <- df %>% mutate(phys_pos=cumsum(length)+position)

I am assuming your data.frame is named df

Or with base R

df$phys_pos <- cumsum(df$length) + df$position
Comments