简体   繁体   English

以时间相关的生存格式格式化重复的日期数据

[英]Formatting repeated date data in time dependent survival format

referencing the following manual for time dependent survival in R:参考以下手册了解 R 中的时间依赖性生存:

https://cran.r-project.org/web/packages/survival/vignettes/timedep.pdf https://cran.r-project.org/web/packages/survival/vignettes/timedep.pdf

From the vignette example:从小插图示例:

subject time1 time2 death creatinine
 5       0     90    0     0.9
 5       90    120   0     1.5
 5       120   185   1     1.2

The data I have is in the following format:我拥有的数据格式如下:

In dd-mm-yyyy format dd-mm-yyyy 格式

subject date           death creatinine
 5       01-01-2022     0     0.9
 5       01-04-2022     0     1.5
 5       01-05-2022     0     1.2
 5        05-07-2022     1     1.2

I need to format the data below to match to the data above.我需要格式化下面的数据以匹配上面的数据。

You can't fill in time2 in the last row without more information.如果没有更多信息,您无法在最后一行填写time2 In single-event data, if an individual has the event (like in your example), time2 value in the final row would typically be the time of the event (in the final row).在单事件数据中,如果一个人有事件(如您的示例中),最后一行中的time2值通常是事件的时间(在最后一行)。 For those that don't have the event, time2 might be the time the observation for that individual ended.对于那些没有事件的人, time2可能是该人的观察结束的时间。

So, excluding the final time2 value per subject , you can do something like this因此,不包括每个subject的最终time2值,您可以执行以下操作

library(dplyr)

df %>% 
  # change date to Date using as.Date()
  mutate(date=as.Date(date,"%d-%m-%y")) %>% 
  # arrange the rows by date
  arrange(date) %>% 
  # group by subject
  group_by(subject) %>% 
  # for each subject, create time2 and time1
  mutate(
    time2 = as.numeric(lead(date)-min(date)-1),
    time1 = lag(time2), 
    time1 = if_else(row_number()==1, 0, time1)
  ) %>% 
  ungroup() %>% 
  # move time1 next to time2
  relocate(time1,.before = time2)

Output: Output:

  subject date       death creatinine time1 time2
    <int> <date>     <int>      <dbl> <dbl> <dbl>
1       5 2020-01-01     0        0.9     0    90
2       5 2020-04-01     0        1.5    90   120
3       5 2020-05-01     1        1.2   120    NA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用R生成具有时间相关协变量的生存数据 - How to generate survival data with time dependent covariates using R 给定生存时间,在 R 中将数据转换为长格式 - Transform data to long format in R given survival time 在生存模型中使用时间相关协变量进行预测 - Predictions using time dependent covariates in survival model 使用&#39;生存&#39;包中的&#39;tmerge&#39;为生存数据添加时变协变量 - Adding time varying covariates to survival data using 'tmerge' in 'survival' package 从入院之日起计算生存时间 - Calculating survival time from the date of hospital addmission Function 为时间相关生存分析创建开始/停止时间变量 - Function to Create Start/Stop Time Variables for Time-Dependent Survival Analysis 合并日期时间列上的数据(POSIXct格式) - Merging Data on date time column (POSIXct format) 编写依赖于日期和/或时间的 Makefile - writing Makefiles that are dependent on date and/or time 从扩展的Cox模型(带有外部时间相关的协变量)获得基线危险函数/生存函数 - Obtain the baseline hazard function/survival function from an extended Cox model (with external time-dependent covariates) 从R?的coxph模型绘制时间相关变量对生存曲线的影响。 - Plot the effect of time dependent variable on survival curve from coxph model in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM