【发布时间】:2021-11-29 17:26:55
【问题描述】:
我有这个数据框:
df1 <- structure(list(ID = c(1, 2, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 9,
10), dateA = structure(c(14974, 18628, 18628, 18628, 14882, 16800,
14882, 17835, 17835, 16832, 16556, 16556, 15949, 16801), class = "Date"),
dateB = structure(c(14610, 15340, 15706, 17501, 14730, NA,
14700, 16191, 17106, 16801, 15810, 16436, 14655, 15431), class = "Date"),
dateC = structure(c(18628, 15705, 17500, 18628, 18628, NA,
18628, 17105, 18628, 18628, 16435, 16556, 15706, 18628), class = "Date")), row.names = c(NA,
-14L), class = c("data.table", "data.frame"))
ID dateA dateB dateC
1: 1 2010-12-31 2010-01-01 2021-01-01
2: 2 2021-01-01 2012-01-01 2012-12-31
3: 2 2021-01-01 2013-01-01 2017-11-30
4: 2 2021-01-01 2017-12-01 2021-01-01
5: 3 2010-09-30 2010-05-01 2021-01-01
6: 4 2015-12-31 <NA> <NA>
7: 5 2010-09-30 2010-04-01 2021-01-01
8: 6 2018-10-31 2014-05-01 2016-10-31
9: 6 2018-10-31 2016-11-01 2021-01-01
10: 7 2016-02-01 2016-01-01 2021-01-01
11: 8 2015-05-01 2013-04-15 2014-12-31
12: 8 2015-05-01 2015-01-01 2015-05-01
13: 9 2013-09-01 2010-02-15 2013-01-01
14: 10 2016-01-01 2012-04-01 2021-01-01
我想检查 dateA 是否在 dateB 和 dateC 的区间内: 我的代码:
library(dplyr)
df1 %>%
mutate(match= ifelse(between(dateA, dateB, dateC), 1, 0))
给予:
Error: Problem with `mutate()` column `match`.
i `match = ifelse(between(dateA, dateB, dateC), 1, 0)`.
x Not yet implemented NAbounds=TRUE for this non-numeric and non-character type
如果我删除包含 NA 的行,则代码有效:
df1 %>%
slice(-6) %>%
mutate(match= ifelse(between(dateA, dateB, dateC), 1, 0))
我想知道,我可以离开NA 所在的行并执行我的代码吗?
【问题讨论】: