[R] Looping survdiff
Terry Therneau
therneau at mayo.edu
Fri Oct 19 15:30:13 CEST 2012
The number of recent questions from umn.edu makes me wonder if there's homework involved....
Simpler for your example is to use get and subset.
dat <- structure(..... as found below
var.to.test <- names(dat)[4:6] #variables of interest
nvar <- length(var.to.test)
chisq <- double(nvar)
for (i in 1:nvar) {
tfit <- survdiff(Surv(time, completion==2) ~ get(var.to.test[i]), data=dat,
subset=(group==3))
chisq[i] <- tfit$chisq
}
write.csv(data.frame(var.to.test, chisq))
On 10/19/2012 05:00 AM, r-help-request at r-project.org wrote:
> 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
More information about the R-help
mailing list