I'm trying to use a conditional lead/lag function in a dplyr pipe using ifelse but getting an error. However, using the same approach outside the pipe it seems to work. What am I missing?
Version info:
R 3.1.1
dplyr_0.4.1
require(dplyr)
Data:
test <- data.frame(a = c("b","b","b","b","b","b",
"m","m","m","m","m","m",
"s","s","s","s","s","s"),
b = replicate(1,n=18),
stringsAsFactors=F)
Dplyr pipe:
test %>%
mutate(delta = ifelse(a=="s",b+lag(b,n=2*6),
ifelse(a=="m",b+lag(b,n=1*6),0)))
--> Error: could not convert second argument to an integer. type=LANGSXP, length = 3
Without the pipe it works:
test$delta <- ifelse(test$a=="s",test$b+lag(test$b,n=2*6),
ifelse(test$a=="m",test$b+lag(test$b,n=1*6),
0))
I found some indication that there was an issue with dplyr lead/lag in combination with grouped data frames. But I am not grouping here.
Aucun commentaire:
Enregistrer un commentaire