我尝试在使用ifelse的dplyr管道中使用条件lead
/ lag
函数,但收到错误。但是,在管道外使用相同的方法似乎有效。我错过了什么?
require(dplyr)
数据:
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
没有管道它可以工作:
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))
我发现dplyr lead
/ lag
与分组数据相结合存在问题。但我不是在这里分组。
版本信息:R 3.1.1和dplyr_0.4.1。
答案 0 :(得分:4)
此:
test %>%
mutate(delta = ifelse(a=="s",b+lag(b,n=12),
ifelse(a=="m",b+lag(b,n=6),0)))
的工作原理。这意味着您无法在lag
参数中传递表达式。
答案 1 :(得分:1)
dplyr
无法解析表达式。一种解决方案是首先定义函数:
foo <- function(a, b)
ifelse(a=="s",b+lag(b,n=2*6), ifelse(a=="m",b+lag(b,n=1*6),0))
test %>% mutate(delta = foo(a,b))