therneau/survival

Cannot add 2nd Surv object to data frame

Closed this issue · 4 comments

Surv objects are not correctly adding to a data frame while using R version 4.2.0 and survival 3.3-1.

Reproducible example:

df <- data.frame(id = c(1:10)
                 , cen1_lo = c(0, 0, 2, 0, 2, 6, 2, 0, 2, 2) 
                 , cen1_hi = c(6, 0, 6, 0, 2, 6, 2, 0, 6, 3)
                 , cen2_lo = c(1, 1, 3, 2, 3, 6, 1, 3, 1, 0) 
                 , cen2_hi = c(6, 3, 6, 2, 4, 6, 2, 5, 6, 3))

df$cen1 <- Surv(df$cen1_lo, df$cen1_hi, type = "interval2")

This yields an expected outcome

   id cen1_lo cen1_hi cen2_lo cen2_hi   cen1
1   1       0       6       1       6 [0, 6]
2   2       0       0       1       3      0
3   3       2       6       3       6 [2, 6]
4   4       0       0       2       2      0
5   5       2       2       3       4      2
6   6       6       6       6       6      6
7   7       2       2       1       2      2
8   8       0       0       3       5      0
9   9       2       6       1       6 [2, 6]
10 10       2       3       0       3 [2, 3]

However, viewing in RStudio seems off--notice last column is labeled cols instead of cen1
image

Proceeding to add 2nd censored variable:

df$cen2 <- Surv(df$cen2_lo, df$cen2_hi, type = "interval2") 

yields the error message:
Error in .subset2(x, i, exact = exact) : subscript out of bounds

But the listing, i.e., df appears ok:

   id cen1_lo cen1_hi cen2_lo cen2_hi   cen1   cen2
1   1       0       6       1       6 [0, 6] [1, 6]
2   2       0       0       1       3      0 [1, 3]
3   3       2       6       3       6 [2, 6] [3, 6]
4   4       0       0       2       2      0      2
5   5       2       2       3       4      2 [3, 4]
6   6       6       6       6       6      6      6
7   7       2       2       1       2      2 [1, 2]
8   8       0       0       3       5      0 [3, 5]
9   9       2       6       1       6 [2, 6] [1, 6]
10 10       2       3       0       3 [2, 3] [0, 3]

However, the viewing in RStudio, i.e., View(df) gives the below error

Error in View : subscript out of bounds

Everything works fine for me. The text below is a simple cut/paste from my terminal. This is of course a clean session, with no extra libraries loaded except survival. (An important pre-condition to any bug report). What else to you have loaded?

> df <- data.frame(id = c(1:10)
+                  , cen1_lo = c(0, 0, 2, 0, 2, 6, 2, 0, 2, 2) 
+                  , cen1_hi = c(6, 0, 6, 0, 2, 6, 2, 0, 6, 3)
+                  , cen2_lo = c(1, 1, 3, 2, 3, 6, 1, 3, 1, 0) 
+                  , cen2_hi = c(6, 3, 6, 2, 4, 6, 2, 5, 6, 3))
> 
> df$cen1 <- Surv(df$cen1_lo, df$cen1_hi, type = "interval2")
> 
> df$cen2 <- Surv(df$cen2_lo, df$cen2_hi, type="interval2")
> df
   id cen1_lo cen1_hi cen2_lo cen2_hi   cen1   cen2
1   1       0       6       1       6 [0, 6] [1, 6]
2   2       0       0       1       3      0 [1, 3]
3   3       2       6       3       6 [2, 6] [3, 6]
4   4       0       0       2       2      0      2
5   5       2       2       3       4      2 [3, 4]
6   6       6       6       6       6      6      6
7   7       2       2       1       2      2 [1, 2]
8   8       0       0       3       5      0 [3, 5]
9   9       2       6       1       6 [2, 6] [1, 6]
10 10       2       3       0       3 [2, 3] [0, 3]
> 

Thanks for your quick response and confirmation the issues are elsewhere.

FYI: seems that RStudio version from 2022 do not "View" data frames with censored data well. I've also posted an RStudio community post here. Reverting to an RStudio version from 2021 seems to address the issue for now.