How to transfer data for survival analysis

Hi there!
I'm trying to set up my data for survival analysis using R. I have four variables T1, T2, T3, T4 each representing a time point where the participants were evaluated. Code 1 and 2 mean that they did not pass the test; code 0 means that they passed the test. I'm trying to transfer this information into two new variables: time (the number of intervals they took to pass the test) and event (whether they passed the test). I'm really new to R and would appreciate any insights regarding how to set up my data! Thank you!

Can you post your data or a sample of it? If the data are in a data frame named DF, you can post the output of either

dput(DF)

or

dput(head(DF, 20))

The latter will give the first 20 rows of data. You can adjust the 20 to a value that makes sense for your data. Paste the output of either of those in a reply and put a line with three back ticks just before and after the output, like this
```
Your output here
```

Sorry it looks pretty messy:
The results of my four time points of the evaluation are X1FLSCRN, X2FLSCRN, X3FLSCRN, and X4FLSCRN

structure(list(X_CHSEX_R = c(1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 
2, 2, 1, 2, 1, 1, 2, 1, 2), X12LANGST = c(1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), P1ANYLNG = c(1, 1, 1, 
NA, 1, 1, 1, 1, 1, 1, 1, NA, 1, 1, 1, 1, 1, NA, 1, 1), P2ANYLNG = c(NA, 
-1, -1, 1, -1, NA, -1, NA, NA, NA, -1, 1, -1, NA, -1, -1, -1, 
1, -1, -1), P4ANYLNG = c(1, 1, 1, 1, 1, 1, 1, NA, 1, 1, 1, 1, 
1, NA, 1, 1, 1, NA, 1, 1), P1BKOTHL = c(1, 1, 3, NA, 1, -1, 2, 
3, 2, 3, 2, NA, 1, 1, 3, 1, 3, NA, 3, 3), P4BKOTHL = c(1, 1, 
1, 3, 1, 3, 1, NA, 3, 3, 3, 3, 2, NA, 3, 1, 1, NA, 1, 1), X1FLSCRN = c(1, 
1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), X2FLSCRN = c(0, 
0, 0, 0, 0, 1, 0, NA, 1, NA, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1), X3FLSCRN = c(0, 
NA, 0, NA, 0, 1, 0, NA, 0, 0, 0, NA, 1, NA, NA, 0, NA, NA, 0, 
NA), X4FLSCRN = c(0, 0, 0, 0, 0, 1, 0, NA, 0, 0, 0, 0, 1, 0, 
0, 0, 0, 0, 0, 0), P1READE1 = c(4, 4, 4, NA, 3, 4, 3, 3, 2, 2, 
4, NA, 2, 4, 3, 1, 4, NA, 2, 4), P1READE2 = c(3, 4, 4, NA, -1, 
2, -1, 3, 2, 1, 3, NA, 4, 3, 3, 4, 4, NA, 3, 2), X2INCCAT_I = c(NA, 
2, 3, 4, 1, NA, 2, NA, NA, NA, 3, 7, 4, NA, 2, 16, 1, 6, 4, 5
), X4INCCAT_I = c(7, 2, 3, 4, 1, 3, 5, NA, 7, 7, 3, 7, 4, NA, 
5, 16, 1, NA, 5, 5), P1HIG_1 = c(14, 7, 13, NA, 12, 7, 16, 9, 
14, 17, 9, NA, 11, 8, 14, 14, 14, NA, 14, 7), P2HIG_1 = c(NA, 
-1, -1, 18, -1, NA, -1, NA, NA, NA, -1, 13, -1, NA, -1, -1, -1, 
14, -1, -1), P1HIG_2 = c(17, 14, 7, NA, -1, -8, -1, 11, 10, 14, 
10, NA, 9, 7, 14, 7, 12, NA, 9, 8), P2HIG_2 = c(NA, -1, -1, 18, 
-1, NA, -1, NA, NA, NA, -1, 7, -1, NA, -1, -1, -1, 14, -1, -1
), X4PAR1RAC = c(3, 4, 3, 5, 3, 3, 4, NA, 3, 3, 3, 3, 3, NA, 
3, 3, 3, NA, 3, 3), X4PAR2RAC = c(-9, -1, 3, 5, -1, 3, -1, NA, 
3, 3, 3, 3, 3, NA, 3, 3, -1, NA, 3, 3), X_RACETH_R = c(3, 3, 
3, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), P1NUMBRS = c(3, 
4, 2, NA, 4, 1, 4, 2, 4, 4, 4, NA, 4, 4, 4, 3, 4, NA, 4, 3), 
    P1READBK = c(4, 4, 2, NA, 3, 1, 4, 2, 2, 3, 3, NA, 1, 3, 
    3, 2, 3, NA, 4, 3), P4READBK = c(3, 1, 4, 2, 2, 2, 3, NA, 
    3, 3, 4, 3, 2, NA, 3, 4, 1, NA, 4, 3), P1RDOTHL = c(2, 2, 
    2, NA, 3, -1, 4, 2, 2, 3, 4, NA, -1, 3, 3, 1, 4, NA, 4, 3
    ), P4RDOTHL = c(3, -1, 2, 1, 2, 4, 3, NA, 3, 1, 4, 2, 2, 
    NA, 3, 1, -1, NA, 4, 1), P1RDMINS = c(30, 60, 30, NA, 30, 
    -1, 30, 10, 20, 15, 10, NA, -1, 60, 10, 20, 25, NA, 60, 20
    ), P4RDMINS = c(20, -1, 20, 15, 20, 60, 20, NA, 20, 30, 10, 
    30, 10, NA, 20, 15, -1, NA, 30, 30), P3RDBKTC = c(2, NA, 
    2, NA, 2, 3, 3, NA, 3, 3, 2, NA, 1, NA, NA, 2, NA, NA, 4, 
    NA), P5RDBKTC = c(3, NA, 2, NA, 4, 1, 3, NA, 2, 3, 2, NA, 
    2, NA, NA, 3, NA, NA, 4, NA), P3HWLGRD = c(3, NA, 1, NA, 
    1, 3, 3, NA, 1, 2, 1, NA, -1, NA, NA, 1, NA, NA, 3, NA), 
    P5HWLGRD = c(1, NA, 1, NA, 2, -1, 2, NA, 1, 2, 2, NA, 1, 
    NA, NA, 2, NA, NA, 2, NA), P3SUMRD = c(-1, NA, 5, NA, -1, 
    -1, -1, NA, 4, 3, -1, NA, -1, NA, NA, -1, NA, NA, -1, NA), 
    P5SUMRD = c(-1, NA, 5, NA, 20, 10, -1, NA, 4, -8, -1, NA, 
    -1, NA, NA, -1, NA, NA, 0, NA), P3RDALON = c(3, NA, 2, NA, 
    4, 4, 3, NA, 2, 3, 2, NA, 3, NA, NA, 2, NA, NA, 4, NA), P5RDALON = c(2, 
    NA, 2, NA, 4, 3, 2, NA, 2, 2, 3, NA, 4, NA, NA, 3, NA, NA, 
    4, NA), Z2NUMBKS = c(NA, NA, NA, NA, NA, 5, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, 4, NA, NA, NA, NA), X1PAR1EMP = c(2, 
    4, 2, NA, 4, 4, 4, 4, 4, 4, 4, NA, 4, 4, 4, 1, 4, NA, 1, 
    4), X1PAR2EMP = c(1, 1, 1, NA, -1, 1, -1, 1, 1, 1, 1, NA, 
    1, 4, 1, 1, 1, NA, 3, 1), X4PAR2EMP_I = c(1, -1, 1, 1, -1, 
    1, -1, NA, 1, 1, 1, 1, 1, NA, 1, 1, -1, NA, 1, 1)), row.names = c(134L, 
189L, 285L, 374L, 684L, 773L, 885L, 1029L, 1038L, 1095L, 1283L, 
1454L, 1868L, 1874L, 1999L, 2018L, 2233L, 2288L, 2327L, 2421L
), class = "data.frame")

I read the data you posted into a data frame named DF. I then selected five columns from it, the first column and the four X#FLSCRN columns. This step is not needed for the data processing but reducing the number of columns allows displaying the results legibly. I used the rowwise() and c_across() functions from dplyr to test if any of the X#FLSCRN values are 0 and which is the first one to be 0. Is that the result you want?

library(dplyr)
DF2 <- select(DF, X_CHSEX_R, X1FLSCRN:X4FLSCRN) #for display purposes only!
DF2 <- DF2 |> rowwise() |> 
   mutate(Event = any(c_across(X1FLSCRN:X4FLSCRN) == 0, na.rm = TRUE),
          Time = which(c_across(X1FLSCRN:X4FLSCRN) == 0)[1])
DF2
# A tibble: 20 x 7
# Rowwise: 
   X_CHSEX_R X1FLSCRN X2FLSCRN X3FLSCRN X4FLSCRN Event  Time
       <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <lgl> <int>
 1         1        1        0        0        0 TRUE      2
 2         2        1        0       NA        0 TRUE      2
 3         2        1        0        0        0 TRUE      2
 4         2        2        0       NA        0 TRUE      2
 5         2        1        0        0        0 TRUE      2
 6         2        1        1        1        1 FALSE    NA
 7         1        1        0        0        0 TRUE      2
 8         1        1       NA       NA       NA FALSE    NA
 9         1        1        1        0        0 TRUE      3
10         1        1       NA        0        0 TRUE      3
11         2        1        1        0        0 TRUE      3
12         2        1        0       NA        0 TRUE      2
13         2        1        1        1        1 FALSE    NA
14         1        1        0       NA        0 TRUE      2
15         2        1        1       NA        0 TRUE      4
16         1        1        0        0        0 TRUE      2
17         1        1        1       NA        0 TRUE      4
18         2        1        0       NA        0 TRUE      2
19         1        1        0        0        0 TRUE      2
20         2        1        1       NA        0 TRUE      4
1 Like

Yes this is exactly what I was trying to do. Thank you so much! I really appreciate it.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.