8

I have data:

dat <- tibble(
         day = 200:210,
         x = sample(-10:10, size = 11,replace = T))

I have a variable y with initial value of 2. I want to calculate the final value of y by adding x to y in a given time step following the following notation:

y[i] = y[i-1] + x

If I did this:

y <- 5
dat %>% mutate(y = y + x)

It adds y to each x.

# A tibble: 11 x 3
    day     x     y
  <int> <int> <dbl>
1   200     4     9
2   201     3     8
3   202    -4     1
4   203    -7    -2
5   204    -3     2
6   205     1     6
7   206    -5     0
8   207    -1     4
9   208    -4     1
10  209    -2     3
11  210     4     9

The answer should be:

  # A tibble: 11 x 3
    day     x     y
  <int> <int> <dbl>
1   200     4     9
2   201     3     12
3   202    -4     8
4   203    -7     1
5   204    -3     -2
6   205     1     -1
7   206    -5     -6
8   207    -1     -7
9   208    -4     -11
10  209    -2     -13
11  210     4     -9

How do I achive this using dplyr package? Or any other method that is quick and fast.

EDIT

If I want to impose a condition such that y cannot exceed 10 or be negative .If it exceeds 10, make it 10 and if it is negative, make it zero. How do I achieve this:

A tibble: 11 x 3

      day     x     y     y1
  1   200     4     9     9
  2   201     3     12    10
  3   202    -4     8     6
  4   203    -7     1     0 
  5   204    -3     -2    0
  6   205     1     -1    0
  7   206    -5     -6    0
  8   207    -1     -7    0  
  9   208    -4     -11   0
  10  209    -2     -13   0
  11  210     4     -9    0
3
  • the command you are looking for: y[i-1] is lag(y) Commented Feb 19, 2018 at 14:18
  • 1
    You can try dat %>% mutate(y = accumulate(x[-n()], ~ .x + .y, .init = 5))
    – akrun
    Commented Feb 19, 2018 at 14:23
  • This does it. I do not understand what (x[-n()] is doing?
    – 89_Simple
    Commented Feb 19, 2018 at 14:28

2 Answers 2

8

We could use accumulate from purrr. With accumulate, do the recursive sum of 'x' elements while initiating with a value of 5 (.init = 5) and remove the first element of accumulate output ([-1])

library(purrr)
library(dplyr)
dat %>%
     mutate(y = accumulate(x, ~ .x + .y, .init = 5)[-1])
# A tibble: 11 x 3
#     day     x      y
#   <int> <int>  <dbl>
# 1   200     4   9.00
# 2   201     3  12.0 
# 3   202    -4   8.00
# 4   203    -7   1.00
# 5   204    -3 - 2.00
# 6   205     1 - 1.00
# 7   206    -5 - 6.00
# 8   207    -1 - 7.00
# 9   208    -4 -11.0 
#10   209    -2 -13.0 
#11   210     4 - 9.00

A similar approach in base R would be

dat$y <- Reduce(function(u, v)  u + v , dat$x, init = 5, accumulate = TRUE)[-1]
dat$y
#[1]   9  12   8   1  -2  -1  -6  -7 -11 -13  -9
9
  • 1
    Very clever solution. Thank you
    – 89_Simple
    Commented Feb 19, 2018 at 14:38
  • ~ .x + .y I cannot get my head around this one. I am trying to read the purrr document but not sure what exactly this bit is doing
    – 89_Simple
    Commented Feb 19, 2018 at 14:52
  • @KS89 Imagine that there are two values you are doing the sum i.e. one you already summed it i.e. 'y' and the other the next value of 'x'
    – akrun
    Commented Feb 19, 2018 at 14:55
  • 1
    @KS89 You will understand the .x and .y if you print it i.e. dat %>% mutate(y = accumulate(x, ~ { print(.x); print(.y); .x + .y; }, .init = 5)[-1])
    – akrun
    Commented Feb 19, 2018 at 15:01
  • Okay. Thanks. I have also edited the question now since I realized I need to impose some conditions on top of it.
    – 89_Simple
    Commented Feb 19, 2018 at 15:14
1
df %>% mutate(y = 5 + cumsum(x))

or, with your extra conditions

df %>% mutate(y = (5 + cumsum(x)) %>% pmin(10) %>% pmax(0))
1
  • Thanks.If a y is converted to 10, the value of 10 should be added in the next x. For e.g. for the first 3 x's (4,3,-4) leads to y = (9, 12,8) which your solution converts to (9,10,8). However, since 12 is converted to 10, 10 should be used to add to -4 and the answer should be (9,10,6). I hope this is clear.
    – 89_Simple
    Commented Feb 19, 2018 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.