Restrict survival time to first five years since diagnosis

Viewed 27

I need to restrict the follow-up time in my survival analysis to five years after diagnosis. However, I'm not sure which function to use.

I'm using the Surv function for the analysis:

coxph(Surv(Time, Event) ~ education, data = data_Cox)

I need to restrict the variable "Time" to a maximum of five years, and after that, the persons need to be censored.

2 Answers

You can use the ifelse() function twice, once to change the Event indicator and once to change the Time variable. You can read the documentation by running ?ifelse or searching online

set.seed(123)
dat <- data.frame(Time = rexp(10, rate = 0.2),
                  Event = rbinom(10, 1, 0.8))
print(dat)
#>          Time Event
#> 1   4.2172863     1
#> 2   2.8830514     0
#> 3   6.6452743     0
#> 4   0.1578868     1
#> 5   0.2810549     1
#> 6   1.5825061     0
#> 7   1.5711365     1
#> 8   0.7263340     1
#> 9  13.6311823     1
#> 10  0.1457672     1
dat$NewTime <- ifelse(dat$Time >= 5,
                      5, dat$Time)
dat$NewEvent <- ifelse(dat$Time >= 5,
                       0, dat$Event)
print(dat)
#>          Time Event   NewTime NewEvent
#> 1   4.2172863     1 4.2172863        1
#> 2   2.8830514     0 2.8830514        0
#> 3   6.6452743     0 5.0000000        0
#> 4   0.1578868     1 0.1578868        1
#> 5   0.2810549     1 0.2810549        1
#> 6   1.5825061     0 1.5825061        0
#> 7   1.5711365     1 1.5711365        1
#> 8   0.7263340     1 0.7263340        1
#> 9  13.6311823     1 5.0000000        0
#> 10  0.1457672     1 0.1457672        1

Created on 2022-09-17 with reprex v2.0.2

Basic idea offered by @DavidLukeThiessen is sound but .. Bad practice to do destructive editing of columns like 'Time' and 'Event' and using ifelse is rather inefficient.

Instead should create a new column. Also could do this

 dat$Event2 <- NA
 dat$Time2 <- NA
 dat$Event2[dat$Time >5] <- 0 
 dat$Time2[ dat$Time > 5 ] <- 5`
Related