2

I have vectors of time, status and gender and would like to do a Survival Analysis:

time <- c(306,455,1010,210,883,1022,310,361,218,166)
status <- c(0,1,0,1,0,0,1,0,1,1)
gender <- c("Male","Male","Female","Female","Male","Female","Female","Female","Female","Female")

A <- survfit(Surv(time, status)~gender)

Then I split the table:

library(broom)
library(dplyr)
B<-tidy(A, censored = TRUE) %>% 
  split(.$strata)

In that table I want certain column so I made a loop:

Time<-list()
Estimate<-list()
StdError<-list()

for (i in 1:length(B)){

  Time[i] <- B[[i]][1]
  Estimate[i] <- B[[i]][5]
  StdError[i] <- B[[i]][6]
}

And when I try to extract and combine the column using cbind and I get this result:

result <- cbind(Time, Estimate, StdError)
>result
      Time      Estimate  StdError 
 [1,] Numeric,7 Numeric,7 Numeric,7
 [2,] Numeric,3 Numeric,3 Numeric,3

Can somebody explain to me why this happened and help me to fix my code so that the output would come out like this:

>result
      Time      Estimate  StdError 
 [1,] 166      0.8571429 0.1543033
      166      0.8571429 0.1543033
      210      0.7142857 0.2390457
      218      0.5714286 0.3273268
      310      0.4285714 0.4364358
      361      0.4285714 0.4364358
      1010     0.4285714 0.4364358
      1022     0.4285714 0.4364358  

 [2,] Time     Estimate   StdError 
      306      1.0       0.0000000
      455      0.5       0.7071068
      883      0.5       0.7071068
mojek
  • 195
  • 1
  • 9

1 Answers1

1

We can use lapply

lapply(B,  function(x) x[c('time', 'estimate', 'std.error')])

Or without anonymous function

lapply(B, `[`, c('time', 'estimate', 'std.error'))
#$`gender=Female`
#  time  estimate std.error
#1  166 0.8571429 0.1543033
#2  210 0.7142857 0.2390457
#3  218 0.5714286 0.3273268
#4  310 0.4285714 0.4364358
#5  361 0.4285714 0.4364358
#6 1010 0.4285714 0.4364358
#7 1022 0.4285714 0.4364358

#$`gender=Male`
#   time estimate std.error
#8   306      1.0 0.0000000
#9   455      0.5 0.7071068
#10  883      0.5 0.7071068

'Time', 'Estimate', and 'StdError' objects are list, we can cbind by looping over the elements of the list with Map

Map(cbind, Time = Time, Estimate = Estimate, StdError = StdError)

If we want to use a for loop, another option is not to create individual vectors, instead subset the dataset directly

out <- vector("list", length(B))
for(i in seq_along(B)) out[[i]] <- B[[i]][c(1, 5, 6)]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Using `lapply` wouldn't be to troubling since we do not have to do any looping. Am I right? – mojek Jul 06 '18 at 07:53
  • @gizmo all of the apply functions use looping, but it is more convenient compared to `for` loop because we don't need to initialize objects for storing output – akrun Jul 06 '18 at 07:54
  • 1
    Thanks @akrun. This really helps. – mojek Jul 06 '18 at 07:57