Example
Here is some data for individual with id = 1:
id time status
--------------
1 t status
t is the time to some event, and status is either 1 if then event occurred or 0 if it did not occurred (in which case t is the duration of the study).
Say that t lies between a2 and a3.
My goal is to transform my data into the following:
id period start stop status
---------------------------
1 1 0 a1 0
1 2 a1 a2 0
1 3 a2 t status
The total time of individual 1 is divided into three intervals where there is no event in (0, a1) and (a1, a2)
Question
Can you think of an efficient way to write an R-function that inputs a data set and a vector a=(a1, a2, ..., aK) and that outputs the transformed data set?
EDIT
Part 1
I have been asked a concrete example. Here is one:
id time status
--------------
1 5 1
and a1=1, a2=3, a3=7.
Part 2 I have also been asked to show my attempt. Here it is
> data <- data.frame(id=1, time=5, status=1)
> a <- c(1, 3, 7)
> N <- nrow(data)
> data$period <- ifelse(data$time < a[1], 1,
+ ifelse(data$time < a[2], 2,
+ ifelse(data$time < a[3], 3, 4)))
>
>
> dataTemp1 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp1) <- names(data)
> dataTemp2 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp2) <- names(data)
> dataTemp3 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp3) <- names(data)
> dataTemp4 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp4) <- names(data)
>
> for(j in 1:N)
+ {
+ if(data[j, "period"] == 1){
+ data[j, "start"] <- 0
+ data[j, "stop"] <- data[j, "time"]
+ } else if(data[j, "period"] == 2){
+ dataTemp1[j, c("id", "time", "period")] <-
+ data[j, c("id", "time", "period")]
+ dataTemp1[j, "start"] <- 0
+ dataTemp1[j, "stop"] <- a[1]
+ dataTemp1[j, "status"] <- 0
+
+ data[j, "start"] <- a[1]
+ data[j, "stop"] <- data[j, "time"]
+ } else if(data[j, "period"] == 3){
+ dataTemp1[j, c("id", "time", "period")] <-
+ data[j, c("id", "time", "period")]
+ dataTemp1[j, "start"] <- 0
+ dataTemp1[j, "stop"] <- a[1]
+ dataTemp1[j, "status"] <- 0
+
+ dataTemp2[j, c("id", "time", "period")] <-
+ data[j, c("id", "time", "period")]
+ dataTemp2[j, "start"] <- a[1]
+ dataTemp2[j, "stop"] <- a[2]
+ dataTemp2[j, "status"] <- 0
+
+ data[j, "start"] <- a[2]
+ data[j, "stop"] <- data[j, "time"]
+ } else if(data[j, "period"] == 4){
+ dataTemp1[j, c("id", "time", "period")] <-
+ data[j, c("id", "time", "period")]
+ dataTemp1[j, "start"] <- 0
+ dataTemp1[j, "stop"] <- a[1]
+ dataTemp1[j, "status"] <- 0
+
+ dataTemp2[j, c("id", "time", "period")] <-
+ data[j, c("id", "time", "period")]
+ dataTemp2[j, "start"] <- a[1]
+ dataTemp2[j, "stop"] <- a[2]
+ dataTemp2[j, "status"] <- 0
+
+ dataTemp3[j, c("id", "time", "period")] <-
+ data[j, c("id", "time", "period")]
+ dataTemp3[j, "start"] <- a[2]
+ dataTemp3[j, "stop"] <- a[3]
+ dataTemp3[j, "status"] <- 0
+
+ data[j, "start"] <- a[3]
+ data[j, "stop"] <- data[j, "time"]
+ }
+ }
>
> dataTemp1 <- dataTemp1[complete.cases(dataTemp1), ]
> dataTemp2 <- dataTemp2[complete.cases(dataTemp2), ]
> dataTemp3 <- dataTemp3[complete.cases(dataTemp3), ]
> dataTemp4 <- dataTemp4[complete.cases(dataTemp4), ]
>
> data <- rbind(data, dataTemp1, dataTemp2, dataTemp3, dataTemp4)
> data[, "period"] <- ifelse(data[, "start"] == 0, 1,
+ ifelse(data[, "start"] == a[1], 2,
+ ifelse(data[, "start"] == a[2], 3,
+ ifelse(data[, "start"] == a[3], 4,
+ 5))))
> data <- data[order(data$id, data$start),
+ c("id", "period", "start", "stop", "status")]
> data
id period start stop status
2 1 1 0 1 0
3 1 2 1 3 0
1 1 3 3 5 1
I’ll write it as a proper reproducible solution: