提问人:Ashish Pandey 提问时间:12/30/2021 最后编辑:jay.sfAshish Pandey 更新时间:12/30/2021 访问量:31
根据条件获取非 NA 日期的差异
get difference of non-na dates based on condition
问:
我尝试构建的逻辑如下
我的表中有 4 个日期列 date,date1,date2,date3
如果 date & date1 为 non-null(不为空),则 x=date-date1 else date & date2 为 non-null, x= date-date2 else date &date3 为非 null,x = date-date3 ..其中 x 是我试图推导的度量。
我已经在 jupyter 中测试了以下代码,它工作正常,因为我能够验证。
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)))
但是当我在GCP中运行此作业时,我出现以下错误
object of type 'closure' is not subsettable "
答:
0赞
jay.sf
12/30/2021
#1
试试这个,如果它能让你前进。应删除 within 的 。df$ttf <-
ifelse
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
数据:
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))
评论
1赞
Ashish Pandey
12/30/2021
是转型吗?我也可以使用相同的 mondf 函数来获取日期差异吗?
0赞
jay.sf
12/30/2021
@AshishPandey 是的,对不起,错别字!
评论