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

Closed
matfournier opened this issue Jan 20, 2015 · 2 comments
Closed

Lag behaves differently inside and outside of mutate #915

matfournier opened this issue Jan 20, 2015 · 2 comments
Assignees
Milestone

Comments

@matfournier
Copy link

@matfournier matfournier commented Jan 20, 2015

From:

http://stackoverflow.com/questions/28038370/lag-changed-in-dplyr-0-4-1-default-parameter-not-working-inside-of-mutate

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
@hadley
Copy link
Member

@hadley hadley commented May 21, 2015

Not a crasher, so putting off until 0.5.

@hadley hadley added this to the 0.5 milestone Jun 1, 2015
@hadley hadley removed this from the 0.4.2 milestone Jun 1, 2015
@tentacles-from-outer-space

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.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants