Logic I am trying to build is as below
I have 4 dates columns in my table date,date1,date2,date3
if date & date1 is non-null(not blank), x=date-date1 else date & date2 is non-null, x= date-date2 else date &date3 are non-null, x = date-date3 .. where x is metric I am trying to derive.
I have tested below code in jupyter and it works fine as I was able to validate.
df$ttf <- ifelse((!is.na(df$date) & !is.na(df$date1)), df$ttf <- mondf(df$date1,df$date),
ifelse((!is.na(df$date) & !is.na(df$date2)), df$ttf <- mondf(df$date2,df$date),
ifelse((!is.na(df$date) & !is.na(df$date3)), df$ttf <- mondf(df$date3,df$date),
NA)))
however when I am running this job in GCP , I am getting below error
object of type 'closure' is not subsettable "
CodePudding user response:
Try this, if it brings you forward. The df$ttf <-
within the ifelse
should be deleted.
df <- transform(df, ttf=ifelse(!is.na(date) & !is.na(date1), date1 - date,
ifelse(!is.na(date) & !is.na(date2), date2 - date,
ifelse(!is.na(date) & !is.na(date3), date3 - date,
NA))))
df
# date date1 date2 date3 ttf
# 1 2021-01-01 2021-03-12 2021-05-21 2021-07-30 70
# 2 2021-01-15 2021-03-26 2021-06-04 2021-08-13 70
# 3 2021-01-29 2021-04-09 2021-06-18 2021-08-27 70
# 4 2021-02-12 2021-04-23 2021-07-02 2021-09-10 70
# 5 2021-02-26 2021-05-07 2021-07-16 2021-09-24 70
# 6 2021-03-12 2021-05-21 2021-07-30 2021-10-08 70
Data:
df <- structure(list(date = structure(c(18628, 18642, 18656, 18670,
18684, 18698), class = "Date"), date1 = structure(c(18698, 18712,
18726, 18740, 18754, 18768), class = "Date"), date2 = structure(c(18768,
18782, 18796, 18810, 18824, 18838), class = "Date"), date3 = structure(c(18838,
18852, 18866, 18880, 18894, 18908), class = "Date")), class = "data.frame", row.names = c(NA,
-6L))