1

I am trying to mutate a new column in mtcars, but make the new entry (row) dependent in on the just-previous row in the same column (the example using mtcars is admittedly nonsense).

Simplify mtcars with...

m.subset <- mtcars[2:7, 10:11]  # top few rows and only the relevant columns

Similar questions have been asked...

Reuse value of previous row during dplyr::mutate

use the diff function with mutate at from dplyr

...and others, but all seem to refer to existing columns, not the subject one -- or they refer to multiple columns/rows as they currently exist.

For example, neither of the following work:

mtcars1 <- mtcars %>% # doesn't work
  mutate (
    newcol = gear + carb + 
      ifelse(shift(newcol, n=1, type = "lag") >4, 0, 1)
  )

mtcars1 <- mtcars %>% # doesn't work
  mutate (
    newcol = gear + carb + 
      ifelse(shift(., n=1, type = "lag") >4, 0, 1)
  )

...and while the following works, it's wrong:

mtcars1 <- mtcars %>% # works but wrong
  mutate (
    newcol = gear + carb + 
      ifelse(lag(.) >4, 0, 1)
  )

Result I am looking for is:

enter image description here

W Barker
  • 324
  • 2
  • 8
  • 1
    why can't a temp column be created with `gear + carb – akrun Mar 30 '22 at 21:35
  • `(lag(gear + carb) <= 4) + gear + carb` ? – Ritchie Sacramento Mar 30 '22 at 21:46
  • 1
    You could wrap with `{}` and use `m.subset %>% mutate(newcol = {tmp <- gear + carb; tmp + ifelse(lag(tmp) > 4, 0, 1)})` – akrun Mar 30 '22 at 21:51
  • 1
    Or may also use `library(pipeR);m.subset %>>% (~tmp = .$gear + .$carb) %>% mutate(newcol = tmp + ifelse(lag(tmp) > 4, 0, 1))` – akrun Mar 30 '22 at 21:54
  • @akrun, thanks for your very helpful comments; I read up on both the use of `{}` and `pipeR`. I'm sure I'll use these at some point. – W Barker Apr 09 '22 at 11:32
  • @akrun, I just asked a follow up question here: https://stackoverflow.com/questions/72147545/dplyr-lags-on-summarised-grouped-data. The new question deals with grouped data. – W Barker May 06 '22 at 21:32

1 Answers1

2

One option doing it in two steps:

library(dplyr)

m.subset %>%
  mutate(newcol = ifelse(gear + carb <= 4, TRUE, FALSE),
         newcol = gear + carb + lag(newcol))

Output

                  gear carb temp
Mazda RX4 Wag        4    4   NA
Datsun 710           4    1    5
Hornet 4 Drive       3    1    4
Hornet Sportabout    3    2    6
Valiant              3    1    4
Duster 360           3    4    8
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • #AndrewGB; thanks. Question, though: I don't see where "temp" is defined. Did you mean to define temp in the first part of the mutate? – W Barker Mar 31 '22 at 01:11
  • 1
    @WBarker You’re right. It should have been newcol. I updated to match the name of your output and forgot to change that part. I’ve corrected it now. – AndrewGB Mar 31 '22 at 01:34
  • 1
    just accepted your answer; it's taken me this long to incorporate your suggestion into the real problem (sometimes perhaps reprex's can be TOO simple!) – W Barker Apr 09 '22 at 11:34