假设我有这些数据:
df <- structure(list(end = structure(c(2932896, 2932896, 17434, 2932896,
2932896, 2932896), class = "Date"), start = structure(c(15397,
16847, 14249, 13801, 12101, 13360), class = "Date")), class = "data.frame", row.names = c(NA,
-6L))
> df
end start
1 9999-12-31 2012-02-27
2 9999-12-31 2016-02-16
3 2017-09-25 2009-01-05
4 9999-12-31 2007-10-15
5 9999-12-31 2003-02-18
6 9999-12-31 2006-07-31
我想创建第三个变量,
dur
,以某个语句为条件:
library(dplyr)
library(lubridate)
df %>%
mutate(dur = if_else(end == "9999-12-31",
as.duration(today() - max("2012-01-01", start)),
as.duration(max(start, "2012-01-01") - end)
)
)
Error in mutate_impl(.data, dots) :
Evaluation error: non-numeric argument to binary operator.
我知道一些建议
DT
ifelse
有约会,但我想呆在房间里。
更新1
这里是专栏
表示预期输出:
| end | start | dur | code |
|------------ |------------ |-------------------------- |-------------------------------------------------- |
| 9999-12-31 | 2012-02-27 | 207100800s (~6.56 years) | as.duration(today()-ymd("2012-02-27")) |
| 9999-12-31 | 2016-02-16 | 81820800s (~2.59 years) | as.duration(today()-ymd("2016-02-16")) |
| 2017-09-25 | 2009-01-05 | 180921600s (~5.73 years) | as.duration(ymd("2017-09-25")-ymd("2012-01-01")) |
| 9999-12-31 | 2007-10-15 | 212025600s (~6.72 years) | as.duration(today()-ymd("2012-01-01")) |
| 9999-12-31 | 2003-02-18 | 212025600s (~6.72 years) | as.duration(today()-ymd("2012-01-01")) |
| 9999-12-31 | 2006-07-31 | 212025600s (~6.72 years) | as.duration(today()-ymd("2012-01-01")) |
更新2
df %>%
mutate(dur = if_else(end == ymd("9999-12-31"),
as.duration(today() - max(ymd("2012-01-01"), start)),
as.duration(max(start, ymd("2012-01-01")) - end)
)
)
生产:
end start dur
1 9999-12-31 2012-02-27 81820800s (~2.59 years)
2 9999-12-31 2016-02-16 81820800s (~2.59 years)
3 2017-09-25 2009-01-05 50716800s (~1.61 years)
4 9999-12-31 2007-10-15 81820800s (~2.59 years)
5 9999-12-31 2003-02-18 81820800s (~2.59 years)
6 9999-12-31 2006-07-31 81820800s (~2.59 years)
更新3(已解决!)
感谢
@jdobres
pmax
而不是
max
. 我想不出原因,但文件上说:
pmax和pmin还将使用适当的比较方法来处理分类的S3或S4对象,is.na和rep(如果需要循环使用参数)
. 我怀疑这与此有关。
df %>%
mutate(dur = if_else(end == ymd("9999-12-31"),
as.duration(today() - pmax(ymd("2012-01-01"), start)),
as.duration(pmax(start, ymd("2012-01-01")) - end)
)
)
end start dur
1 9999-12-31 2012-02-27 207100800s (~6.56 years)
2 9999-12-31 2016-02-16 81820800s (~2.59 years)
3 2017-09-25 2009-01-05 180921600s (~5.73 years)
4 9999-12-31 2007-10-15 212025600s (~6.72 years)
5 9999-12-31 2003-02-18 212025600s (~6.72 years)
6 9999-12-31 2006-07-31 212025600s (~6.72 years)