Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird behaviour of lag function of dplyr inside mutate

Tags:

r

dplyr

lag

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?

like image 264
AntoniosK Avatar asked Nov 04 '15 12:11

AntoniosK


1 Answers

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.

like image 192
RDavey Avatar answered Oct 20 '22 18:10

RDavey