AntoniosK
AntoniosK

Reputation: 16121

Weird behaviour of lag function of dplyr inside mutate

I guess it is correlated to that story : Changing behaviour of stats::lag when loading dplyr package , but I've found some weird behaviour of lag function when I try to use the default = option.

Check the simple commands below

library(dplyr)

df = data.frame(mtcars)

df %>% mutate(lag_cyl = lag(cyl))
## it works with NA in first value (as expected)

df %>% mutate(lag_cyl = lag(cyl, default = 999))
## it works with a given value as default

df %>% mutate(lag_cyl = lag(cyl, default = cyl[1]))
## it DOESN'T WORK with the first value of the column as default

df %>% mutate(lag_cyl = dplyr::lag(cyl, default = cyl[1]))
## it works when specifying dplyr::

val = df$cyl[1]
df %>% mutate(lag_cyl = lag(cyl, default = val))
## it works when I assign the first value of the column to a variable name

Is it just a conflict between the stats and dplyr packages?

Upvotes: 6

Views: 2411

Answers (1)

RDavey
RDavey

Reputation: 1909

I can confirm that df %>% mutate(lag_cyl = lag(cyl, default = cyl[1])) works in dplyr version 0.8.0.1.

If it worked when specifying dplyr::lag it suggests that another library was masking the function. Hmisc has a function called lag, and you may have loaded this library (or another one with a lag function) after loading dplyr.

Upvotes: 3

Related Questions