[R] convenient way to combine Surv objects?
Heinz Tuechler
tuechler at gmx.at
Sun Dec 10 00:52:26 CET 2006
Dear All,
is there a convenient way to combine Surv objects?
Imagine two Surv objects as in the following example.
Is it possible to combine them into one _Surv_ object?
I tried rbind(), cbind(), c(), merge(), but none of these function does,
what I would like.
So I drafted a method for rbind.Surv (see below), but I would be happy
about a better solution.
Ideas? Comments?
Thanks,
Heinz
R version 2.4.0 Patched (2006-11-03 r39792)
Windows XP
library(survival)
## create example data
so1 <- Surv(1:5, c(0, 0, 1, 0, 1))
so2 <- Surv(6:8, c(1, 0, 0))
### "combinefunction"(so1, so2)
## desired result:
[1] 1+ 2+ 3 4+ 5 6 7+ 8+
### Surv method for rbind - untested
rbind.Surv <- function(..., deparse.level = 1)
{
so <- list(...)
## check objects for number of columns
so.dims <- sapply(so, dim)[2, ] # colums of each so
min.so.cols <- min(so.dims)
max.so.cols <- max(so.dims)
## if max range == 3 insert zero column in so with only 2 columns
if (min.so.cols == 2 & max.so.cols == 3)
for (i in seq(along=so)) {
if (dim(so[[i]])[2] == 2) so[[i]] <- cbind(0, so[[i]])
}
## function to get column in list element
elco <- function(element,column) {element[,column]}
if (max.so.cols == 2)
return(Surv(unlist(sapply(so, elco, 1)), unlist(sapply(so, elco, 2))))
else
return(Surv(unlist(sapply(so, elco, 1)), unlist(sapply(so, elco, 2)),
unlist(sapply(so, elco, 3))))
}
rbind(so1, so2)
More information about the R-help
mailing list