Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lag behaves differently inside and outside of mutate #915

matfournier opened this issue Jan 20, 2015 · 2 comments

Lag behaves differently inside and outside of mutate #915

matfournier opened this issue Jan 20, 2015 · 2 comments


Copy link


Trivial example that worked in previous dplyr versions but not 0.4.1, as the first row is left as NA rather than the default value:

blah <- data.frame(x1 = c(5,10,20,27,35,58,5,6))
blah <- mutate(blah, x2 = x1 - lag(x1, n=1, default=0))

Will return NA on the first row. Oddly lag(x1, default=0) works as intended.

Doing the above code outside of the mutate function works fine (accepts default=x param):
e.g. blah$x2 <- blah$x1 - lag(blah$x1, n=1, default=0) works as intended.

Thus lag works differently inside and outside of mutate.

@hadley hadley added this to the 0.4.2 milestone May 19, 2015
@hadley hadley self-assigned this May 19, 2015
Copy link

hadley commented May 21, 2015

Not a crasher, so putting off until 0.5.

@hadley hadley modified the milestones: 0.5, 0.4.2 Jun 1, 2015

Just to notice. Same thing happens with order_by. Following example from help("lead"):

right <- mutate(scrambled, prev = lag(value, order_by = year))
arrange(right, year)

doesn't work as expected when n is added:

right <- mutate(scrambled, prev = lag(value, n=1L, order_by = year))
arrange(right, year)

@lock lock bot locked as resolved and limited conversation to collaborators Jun 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

No branches or pull requests

4 participants