I have the following dataframe:
df <-read.table(header=TRUE, text="id time event
1 90 1
2 87 0
3 50 1
4 54 0
5 30 0
6 15 1
7 20 1
8 20 0
9 12 0
10 13 1")
I would like to create a new column in this dataframe with the probabilities of survival (simple kaplan-Meier function) for those individuals above an age threshold (i.g. 30) and who do not have the event. In the data above, it would be patients 2 and 4 only.
So far I have the following:
library(survival)
Censoring_time <- function(
TIME = 30){
km_fit <- survfit(Surv(end, event) ~ 1, data=df)
if(time > TIME & event = 0){
df$New_Event <- km_fit
}
else(df$New_Event <- df$event)
}