【问题标题】:replace Dates with "no answer" and "too early"用“没有答案”和“太早”替换日期
【发布时间】:2021-12-20 16:43:15
【问题描述】:

我正在尝试找出调查中的日期。该调查有 4 个阶段,基线、30 天、60 天和 90 天,超过 1000 行。基线阶段是我想要作为参考的阶段,以检查它是否在 30,60 和 90 天更新。如果在 30/60/90days 中更新了超过 30/60/90days 的列,则没有问题,如果它有一个有效的日期。

两个可能的答案:

  1. 没有答案:这意味着如果我们添加 30/60/90 天,我没有任何日期
  2. 太早:这意味着如果我们添加 30/60/90 天,则从基准日期开始响应太早了

目前 df 看起来像这样:

Baseline Dates_30d Dates_60d Dates_90d
2019-06-01 2019-07-1 NA NA
2019-06-03 2019-07-3 NA NA
2019-05-20 NA NA NA
2019-07-01 2019-08-1 2019-09-1 2019-10-1
2019-05-01 2019-06-1 2019-07-1 NA

而我想要的是:

Baseline Dates_30d Dates_60d Dates_90d
2019-06-01 2019-07-1 too early too early
2019-06-03 2019-07-3 No answer No answer
2019-05-20 No answer No answer No answer
2019-07-01 2019-08-1 2019-09-1 too early
2019-05-01 2019-06-1 2019-07-1 No answer

解释:

对于第一行:基线阶段发生在 2019 年 6 月 1 日,2019 年 7 月 1 日的 30 天,但对于 60 和 90 天的响应还为时过早。

对于第二行:基线阶段发生在 2019-06-03,30_d 发生在 2019-07-03 但是对于 60&90days 列已经过了 60 和 90 天,因此可以回答是“否回答”。

【问题讨论】:

  • 我不明白第 1 行和第 2 行在处理上的区别。在每一行中,似乎基线中有一个数字,Dates_30d 中有一个日期,即 29 天后,但看起来就像您希望在每种情况下的最后一列中都有不同的输出。你能解释一下吗,也许通过几个示例行的计算?
  • 抱歉,第 1 行和第 2 行的 Dates_30 列应分别为 2019-07-01 和 2019-07-03。
  • 请更新您的问题,以便输入数据和所需结果反映您的期望。请添加一些解释,因为我不明白为什么某些行的 Dates_90d 在输入似乎具有相似条件时有“太早”和“无答案”。
  • 是的,请添加说明。在第 3 列中,第 1 行是“太早”,在第 3 列中,第 2 行是“没有答案”,即使看起来第 2 行的受访者的截止日期晚于第 1 行的受访者的截止日期 (2019-08-01我猜第 1 行的受访者和 2019-08-03 第 2 行的受访者)。真的不清楚,什么时候应该“不回答”,什么时候“太早”的规则是什么

标签: r date


【解决方案1】:

没有明确的参考日期是不可能的 分配状态“无应答”或“太早”。例如,如果今天是 2021 年 11 月 8 日和数据框中的 Dates_90d 列是 NA 则“没有答案”,但如果今天是 2019 年 8 月 1 日,则“为时过早”。 我冒昧地选择了 2019 年 8 月 1 日作为参考。

library(tidyverse)
library(lubridate)

df <- read_table("
Baseline  Dates_30d Dates_60d Dates_90d
2019-06-01  2019-07-01 NA  NA
2019-06-03  2019-07-03  NA  NA
2019-05-20  NA  NA  NA
2019-07-01  2019-08-01 2019-09-01   2019-10-01
2019-05-01  2019-06-01  2019-07-01  NA", col_types = "DDDD")


# I put for the sake of the test 
# the date which is relevent to 
# the time frame of the survey.
# in general it should be
# tday <- today()
tday <- ymd("2019-08-01")

early <- function(bl, date, n, tday) {
  
  if(!is.na(date)) 
    r <- as.character(date)
  else if(tday - bl < n)
    r <- "too early"
  else
    r <- "no answer"
   r
}

df %>% rowwise() %>%
  mutate(status_30d = early(Baseline, Dates_30d, 30, tday),
         status_60d = early(Baseline, Dates_60d, 60, tday),
         status_90d = early(Baseline, Dates_90d, 90, tday))

输出:

# A tibble: 5 x 7
# Rowwise: 
  Baseline   Dates_30d  Dates_60d  Dates_90d  status_30d status_60d status_90d
  <date>     <date>     <date>     <date>     <chr>      <chr>      <chr>     
1 2019-06-01 2019-07-01 NA         NA         2019-07-01 no answer  too early 
2 2019-06-03 2019-07-03 NA         NA         2019-07-03 too early  too early 
3 2019-05-20 NA         NA         NA         no answer  no answer  too early 
4 2019-07-01 2019-08-01 2019-09-01 2019-10-01 2019-08-01 2019-09-01 2019-10-01
5 2019-05-01 2019-06-01 2019-07-01 NA         2019-06-01 2019-07-01 no answer 

【讨论】:

  • 每次参考日期都不一样。所以它需要每次检查baseline date,然后更新为“no answer”或“too early”。
  • 很遗憾无法得出结论。例如,我在 2021 年 1 月 1 日做了一个调查,等待调查参与者的跟进。我知道在 2 月 1 日之前我需要获得反馈,但不知道今天是什么日期,我无法判断是“太早”还是“太晚”。因此,您需要添加添加参考日期的列(每行的“今天”)。
猜你喜欢
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多