【问题标题】:r : calculate time interval to referencer : 计算时间间隔以供参考
【发布时间】:2023-01-22 11:54:32
【问题描述】:

我想请求你的帮助。

tibble::tribble(
      ~Last.TP,  ~D.EndStudy,    ~EOTvisit,       ~D.END,        ~D.IP,
  "2021-02-08", "2020-08-13", "2020-08-13", "2020-08-13", "2019-07-24",
  "2021-04-19", "2021-04-26", "2021-04-26", "2021-04-26", "2020-04-06",
  "2022-01-24", "2022-02-10", "2022-02-10", "2022-02-10", "2021-01-11"
  )

所需的临时输出低于 tibble。 在下面的标题中, 天数是参考 D.IP 计算的 (2021-02-08 - 2019-07-24) + 1 = 566(天) 我想低于 tibble,但在几个月内。 非常感谢你!

tibble::tribble(
  ~LastTP, ~DENDSTUDY, ~EOTVISIT, ~DEND, ~DIP,
     566L,       387L,      387L,  387L,   1L,
     636L,       643L,      643L,  643L, 258L,
     916L,       933L,      933L,  933L, 538L
  )

【问题讨论】:

    标签: r time


    【解决方案1】:

    我们可以使用 tripple across 来做到这一点:

    1. 首先我们将字符类转换为日期类。
    2. acrossD.IP的第一行减去所有进行计算
    3. 我们再次在所有列中根据天数计算月份:
      library(dplyr)
      library(lubridate)
      df %>% 
        mutate(across(, ymd),
               across(, ~ . - D.IP[1])+1,
               across(, ~ as.numeric(round(./30.417, digit=1)))
               )
      
       Last.TP D.EndStudy EOTvisit D.END  D.IP
          <dbl>      <dbl>    <dbl> <dbl> <dbl>
      1    18.6       12.7     12.7  12.7   0  
      2    20.9       21.1     21.1  21.1   8.5
      3    30.1       30.7     30.7  30.7  17.7
      

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2013-07-16
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      相关资源
      最近更新 更多