Thank you for all your responses, I assure you this is not homework. I am a graduate student and my classes are complete. I am trying multiple different ways to analyze data and my lab requests different types of scripts to accomplish various tasks. I am the most computer savy in the lab so it comes to me. I am continually trying to learn more about using R and I truly value all the suggestions. Again, thank you for your assistance,
Regards, Charles On Fri, Oct 19, 2012 at 8:30 AM, Terry Therneau <thern...@mayo.edu> wrote: > 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-requ...@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 >> > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.