我有以下数据框,我试图获得低于预期的输出。我正在尝试将EST_INST_ST_DATE
与ST_DATE
进行比较(如果匹配)我想保留IS_WITHIN_30DAYS
列指示符1,如果EST_INST_ST_DATE
位于我希望拥有1的月份之间接下来的两个月
INPUT
ID ST_DATE ND_DATE EST_INST_ST_DATE
1 2016-01-04 2016-02-03 2016-01-04
1 2016-02-04 2016-03-03 2016-01-04
2 2016-01-04 2016-02-03 2016-01-12
2 2016-02-04 2016-03-03 2016-01-12
2 2016-03-04 2016-04-03 2016-01-12
Expected output
ID ST_DATE ND_DATE EST_INST_ST_DATE IS_WITHIN_30DAYS
1 2016-01-04 2016-02-03 2016-01-04 1
1 2016-02-04 2016-03-03 2016-01-04 0
2 2016-01-04 2016-02-03 2016-01-12 1
2 2016-02-04 2016-03-03 2016-01-12 1
2 2016-03-04 2016-04-03 2016-01-12 0
我正在尝试以下代码但我收到错误
chargefmv5<- chargefmv5 %>%
mutate(IS_WITHIN_30DAYS=ifelse((EST_INST_ST_DATE == ST_DATE),1,
ifelse((EST_INST_ST_DATE > ST_DATE & EST_INST_ST_DATE <ND_DATE)),1,0))
Error in ifelse((EST_INST_ST_DATE == ST_DATE), 1, ifelse((EST_INST_ST_DATE > :
unused arguments (1, 0)
答案 0 :(得分:1)
您提前关闭了第二个ifelse
。尝试:
chargefmv5 <- chargefmv5 %>%
mutate(IS_WITHIN_30DAYS=ifelse(
(EST_INST_ST_DATE == ST_DATE),
1,
ifelse(
(EST_INST_ST_DATE > ST_DATE & EST_INST_ST_DATE <ND_DATE),
1,
0
)
)
)