[R] looping survdiff?

Andrews, Chris chrisaa at med.umich.edu
Fri Oct 19 13:46:25 CEST 2012


> sapply(seq(4,ncol(dat)), function(i) survdiff(Surv(time,completion=="2")~dat[,i], data=dat, subset=group=="3")$chisq)
[1] 0.0944 4.4854 3.4990

Chris

-----Original Message-----
From: Charles Determan Jr [mailto:deter088 at umn.edu] 
Sent: Thursday, October 18, 2012 3:04 PM
To: r-help at r-project.org
Subject: [R] looping survdiff?

Hello,

I am trying to set up a loop that can run the survdiff function with the ultimate goal to generate a csv file with the p-values reported.  However, whenever I try a loop I get an error such as "invalid type (list) for variable 'survival_data_variables[i]".

This is a subset of my data:

structure(list(time = c(1.51666666666667, 72, 72, 25.7833333333333, 72, 72, 72, 72, 72, 72, 1.18333333333333, 0.883333333333333, 1.15, 0.866666666666667, 72, 1.03333333333333, 72, 1.05, 72, 22.7666666666667), group = c(2L, 1L, 3L, 3L, 3L, 4L, 4L, 1L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L), completion = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor"), var1 = structure(c(2L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 2L, 4L, 3L, 2L, 4L, 2L, 4L, 2L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"),
    var2 = structure(c(3L, 3L, 1L, 1L, 2L, 4L, 3L,
    3L, 2L, 4L, 2L, 1L, 2L, 1L, 2L, 2L, 4L, 4L, 2L, 3L), .Label = c("1",
    "2", "3", "4"), class = "factor"), var3 = structure(c(4L,
    2L, 3L, 1L, 3L, 4L, 4L, 2L, 2L, 4L, 2L, 2L, 1L, 2L, 2L, 2L,
    1L, 3L, 4L, 1L), .Label = c("1", "2", "3", "4"), class = "factor")), .Names = c("time", "group", "completion", "var1", "var2", "var3"), row.names = c(NA, 20L), class = "data.frame")


The loop I have been trying for just group 3 is:

d=data.frame()
for(i in 4:6){
    a=assign(paste("p-value",i,sep=""),
        survdiff(Surv(time, completion=="2")~dat[i],
        data=dat[group=="3",],
        rho=0))
    b=as.matrix(a$chisq)
    d=rbind(d, b)
write.csv(d, file="C:/.../junk.csv", quote=FALSE)}

Perhaps I am making this more difficult than it needs to be.  Thanks for any help,

Charles

	[[alternative HTML version deleted]]


**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues 




More information about the R-help mailing list