I am very new to R. I have some data from a CVS stored in vdata with 4 columns labeled: X08, Y08, X09, Y09. I have created two new "columns" like so: Z08 <- (vdata$X08-vdata$Y08) Z09 <- (vdata$X09-vdata$Y09) I would like to use chisq.test for each "row" and output the p-value for each in a stored variable. I don't know how to do it. Can you help? so far I have done it for one row (but I want it done automatically for all my data): chidata=rbind(c(vdata$Y08[1],Z08[1]),c(vdata$Y09[1],Z09[1])) results <- chisq.test(chidata) results$p.value I tried removing the [1] and the c() but that didn't work... Any ideas? THANKS!
On May 9, 2009, at 4:53 PM, JC wrote:> > I am very new to R. I have some data from a CVS stored in vdata with 4 > columns labeled: > X08, Y08, X09, Y09. > > I have created two new "columns" like so: > > Z08 <- (vdata$X08-vdata$Y08) > > Z09 <- (vdata$X09-vdata$Y09) > > I would like to use chisq.test for each "row"Of what?> and output the p-value > for each in a stored variable. I don't know how to do it. Can you > help? > > so far I have done it for one row (but I want it done automatically > for all my data): > > chidata=rbind(c(vdata$Y08[1],Z08[1]),c(vdata$Y09[1],Z09[1]))Maybe I am dense, but I cannot figure out what hypothesis is being tested.> > results <- chisq.test(chidata) > results$p.valueGenerally using apply(vdata, 1, ..... would give you a row by row computation.> > > I tried removing the [1] and the c() but that didn't work... Any > ideas?As Jim Holtman's tag line says: "What problem are you trying to solve?" David Winsemius, MD Heritage Laboratories West Hartford, CT
JC, If each row are the counts for a 2 x 2 contingency table - so for the ith contingency table you have counts for row 1 c(Y08[i],Z08[i]) and row 2 (Y09[i],Z09[i]) then you could use apply: X <- cbind(vdata$Y08,vdata$X08-vdata$Y08,vdata$Y09,vdata$X09-vdata$Y98) f.chisq <- function(x){ m <- matrix(x,2,2) chisq.test(m)$p.value } apply(X,1,f.chisq) Quoting JC <jerome.coste at gmail.com>:> > I am very new to R. I have some data from a CVS stored in vdata with 4 > columns labeled: > X08, Y08, X09, Y09. > > I have created two new "columns" like so: > > Z08 <- (vdata$X08-vdata$Y08) > > Z09 <- (vdata$X09-vdata$Y09) > > I would like to use chisq.test for each "row" and output the p-value > for each in a stored variable. I don't know how to do it. Can you > help? > > so far I have done it for one row (but I want it done automatically > for all my data): > > chidata=rbind(c(vdata$Y08[1],Z08[1]),c(vdata$Y09[1],Z09[1])) > results <- chisq.test(chidata) > results$p.value > > I tried removing the [1] and the c() but that didn't work... Any > ideas? > > THANKS! > > ______________________________________________ > R-help at 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. >