[R] How to join data.frames containing Surv objects?
Heinz Tuechler
tuechler at gmx.at
Tue Dec 5 17:04:36 CET 2006
Dear All,
Trying to combine two data frames with identical structure by rbind() or
merge() I cannot find a way to preserve the class of a Surv object (see
example).
Reading the help page for rbind, I an uncertain if I could expect that a
Surf oject retains it's class, but I would wish it did.
Thanks
Heinz Tüchler
R version 2.4.0 Patched (2006-11-03 r39792)
Windows XP
library(survival)
## create example data
starttime <- rep(0,5)
stoptime <- 1:5
event <- c(1,0,1,1,1)
group <- c(1,1,1,2,2)
## build Surv object
survobj <- Surv(starttime, stoptime, event)
## build data.frame with Surv object
df.test <- data.frame(survobj, group); df.test; str(df.test)
## split in two data frames
dft1 <- df.test[1:3,]; dft1; str(dft1); class(dft1$survobj) # class is Surv
dft2 <- df.test[4:5,]; dft2; str(dft2); class(dft2$survobj) # class is Surv
## rbind in one data.frame
dft12 <- rbind(dft1, dft2); dft12; str(dft12); class(dft12$survobj) #
class is matrix
## merge in one data.frame
dft12merge <- merge(dft1, dft2, all=TRUE, sort=FALSE)
dft12merge; str(dft12merge); class(dft12merge$survobj) # class is matrix
More information about the R-help
mailing list