连接日期变量的元素时使用 ifelse 语句

Using ifelse statement when concatenating elements of a date variable

我正在尝试使用两个 ifelse 语句创建一个新的日期变量,该变量做出一系列假设来填补现有日期变量的空白。这是我的意思的一个例子:

  id EffectiveDate EffectiveYear ED_NA EY_NA NewEffectiveDate
1  a    1972-10-05          1972 FALSE FALSE       1972-10-05
2  a          <NA>          1985  TRUE FALSE       1985-01-01
3  a    1988-11-12          1988 FALSE FALSE       1988-11-12
4  b    2011-09-05          2011 FALSE FALSE       2011-09-05
5  b          <NA>            NA  TRUE  TRUE       2011-09-05
6  b          <NA>          2012  TRUE FALSE       2012-01-01
7  c    2012-11-11          2012 FALSE FALSE       2012-11-11
8  c    2013-05-15          2013 FALSE FALSE       2013-05-15

id:EY_NA =

的快速代码
id <- c("a","a","a","b","b","b","c","c")
EffectiveDate <- c("1972-10-05",NA,"1988-11-12","2011-09-05",NA,NA,"2012-11-11","2013-05-15")
EffectiveYear <- c(1972,1985,1988,2011,NA,2012,2012,2013)
tdat <- data.frame(id, EffectiveDate, EffectiveYear)
tdat$ED_NA <- is.na(tdat$EffectiveDate)
tdat$EY_NA <- is.na(tdat$EffectiveYear)

我在这个例子中试图创建的是 "NewEffectiveDate" 变量。用简单的英语来说,我想要的是,如果 EffectiveDate 数据丢失但 EffectiveYear 数据不丢失,假设 NewEffectiveDate 等于 EffectiveYear 的 1 月 1 日。如果缺少 EffectiveDate 和 EffectiveYear 数据,则假定先前观察的 EffectiveDate。最后,当然,如果 EffectiveDate 数据没有丢失,select EffectiveDate.

这是我用来尝试解决问题的最新代码:

tdat %>% mutate(NewEffectiveDate = ifelse(ED_NA == 1 & EY_NA == 0,
  as.Date(paste(EffectiveYear, 1, 1, sep="-")),
  ifelse(ED_NA == 1 & EY_NA == 1), 
  as.Date(lag(EffectiveDate)),
  EffectiveDate
))

当我尝试这个特定代码时,我收到一条错误消息:错误:未使用的参数 (as.Date(c(NA, 1, NA, 2, 3, NA, NA, 4)) , c(1, 不适用, 2, 3, 不适用, 不适用, 4, 5))

我用 "ifelse concatenate date" 之类的查询及其一些变体搜索了类似的问题,但没有找到任何似乎适用于这个特定问题的内容。

我是 R(和 CLI,就此而言)的新手,所以如果我忽略了一个非常明显的解决方案,我提前道歉。从 Excel 到 R 的过渡很有趣,但在执行看似相对简单的任务时常常很痛苦(尽管 dplyr 包非常有帮助)。

你的 ifelse 块似乎有问题你提前关闭了第二个块的括号并且没有给出 yesno 参数并且你给出了一个第一个 ifelse 块的额外参数。

这应该有效:

tdat %>% mutate(NewEffectiveDate = ifelse(ED_NA == 1 & EY_NA == 0,
  as.Date(paste(EffectiveYear, 1, 1, sep="-")),
  ifelse(ED_NA == 1 & EY_NA == 1, 
  as.Date(lag(EffectiveDate))),
  EffectiveDate))
id <- c("a","a","a","b","b","b","c","c")
EffectiveDate <- c("1972-10-05",NA,"1988-11-12","2011-09-05",NA,NA,"2012-11-11","2013-05-15")
EffectiveYear <- c(1972,1985,1988,2011,NA,2012,2012,2013)
tdat <- data.frame(id, EffectiveDate, EffectiveYear,
                   stringsAsFactors=FALSE)

library(zoo)
tdat %>% 
  mutate(NewEffectiveDate = ifelse(!is.na(EffectiveDate),
                                   EffectiveDate,
                                   ifelse(is.na(EffectiveDate) & !is.na(EffectiveYear),
                                          paste0(EffectiveYear, "-01-01"),
                                          NA)),
         NewEffecitveDate = na.locf(NewEffectiveDate))

这应该能满足您的需求。我建议使用 zoo 包中的 na.locf(最后一个结转),而不是尝试处理前一个日期问题。

你可以做到

tdat$EffectiveDate <- as.Date(tdat$EffectiveDate)

tdat %>% mutate(NewEffectiveDate = as.Date(
    ifelse(!is.na(EffectiveDate), EffectiveDate,
           ifelse(!is.na(EffectiveYear), as.Date(paste(EffectiveYear, 1, 1, sep="-")),
                  lag(EffectiveDate)))
)) -> res

res
#   id EffectiveDate EffectiveYear NewEffectiveDate
# 1  a    1972-10-05          1972       1972-10-05
# 2  a          <NA>          1985       1985-01-01
# 3  a    1988-11-12          1988       1988-11-12
# 4  b    2011-09-05          2011       2011-09-05
# 5  b          <NA>            NA       2011-09-05
# 6  b          <NA>          2012       2012-01-01
# 7  c    2012-11-11          2012       2012-11-11
# 8  c    2013-05-15          2013       2013-05-15