我想请求您的帮助。
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"
)
所需的临时输出低于小标题。 在下面的小标题中, 天数计算引用D.IP
(2021-02-08 - 2019-07-24) + 1 = 566 (days)
(2021/04/19 - 2020/04/06) + 1 = 379 (days)
我想低于 tibble,但需要几个月的时间。
tibble::tribble(
~LastTP, ~DENDSTUDY, ~EOTVISIT, ~DEND, ~DIP,
566L, 387L, 387L, 387L, 1L,
379L, 386L, 386L, 386L, 1L,
379L, 396L, 396L, 396L, 1L
)
非常感谢!
最佳答案
更新:OP请求:
library(lubridate)
library(dplyr)
df %>%
mutate(across(4:8, ~ . - D.IP[1])+1,
across(4:8, ~ as.numeric(round(./30.417, digit=1)))
)
第一个答案:
我们可以使用三重across
来做到这一点:
- 首先,我们将字符类转换为日期类。
- 通过
across
进行计算,从D.IP
的第一行中减去所有内容 - 我们再次在所有列中根据天数计算月份:
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
关于r : calculate time interval to reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75186469/