3
votes

I have a dataset: (actually I have more than 100 groups) enter image description here

and I want to use dplyr to create a variable-y for each group, and fill first value of y to be 1,

Second y = 1* first x + 2*first y

The result would be: enter image description here

I tried to create a column- y, all=1, then use

df%>%   group_by(group)%>%   mutate(var=shift(x)+2*shift(y))%>% ungroup()

but the formula for y become, always use initialize y value--1

Second y = 1* first x + 2*1

Could someone give me some ideas about this? Thank you!

The dput of my result data is:

structure(list(group = c("a", "a", "a", "a", "a", "b", "b", "b" ), x = c(1, 2, 3, 4, 5, 6, 7, 8), y = c(1, 3, 8, 19, 42, 1, 8, 23)), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame" ))

1

1 Answers

3
votes

To perform such calculation we can use accumulate from purrr or Reduce in base R.

Since you are already using dplyr we can use accumulate :

library(dplyr)

df %>%
  group_by(group) %>%
  mutate(y1 = purrr::accumulate(x[-n()], ~.x * 2 +  .y, .init = 1))

#  group     x     y    y1
#  <chr> <dbl> <dbl> <dbl>
#1 a         1     1     1
#2 a         2     3     3
#3 a         3     8     8
#4 a         4    19    19
#5 a         5    42    42
#6 b         6     1     1
#7 b         7     8     8
#8 b         8    23    23