Dear list, I'd like to do several t-test within a for loop. Example follows: data1 <- rnorm(1:25) data2 <- rnorm(1:25) vars <- c("data1", "data2") for (i in vars) { t.test(i) } Unfortunately, it does not work because of the quotes in the vars-vector (running t.test(data1) by hand works). How can I remove the quotes before performing the test? Sincerely, Arne Schulz
On Jul 22, 2010, at 1:28 PM, Arne Schulz wrote:> Dear list, > I'd like to do several t-test within a for loop. Example follows: > > data1 <- rnorm(1:25) > data2 <- rnorm(1:25) > vars <- c("data1", "data2") > > for (i in vars) { > t.test(i) > } > > Unfortunately, it does not work because of the quotes in the vars- > vector (running t.test(data1) by hand works). > > How can I remove the quotes before performing the test?a) don't quote them b) put them in a list c) print() the results. -- David Winsemius, MD West Hartford, CT
Try this: apply(Vectorize(get)(ls(patt = 'data')), 2, t.test) On Thu, Jul 22, 2010 at 2:28 PM, Arne Schulz < arne.schulz@student.uni-kassel.de> wrote:> Dear list, > I'd like to do several t-test within a for loop. Example follows: > > data1 <- rnorm(1:25) > data2 <- rnorm(1:25) > vars <- c("data1", "data2") > > for (i in vars) { > t.test(i) > } > > Unfortunately, it does not work because of the quotes in the vars-vector > (running t.test(data1) by hand works). > > How can I remove the quotes before performing the test? > > Sincerely, > Arne Schulz > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On 2010-07-22 11:28, Arne Schulz wrote:> Dear list, > I'd like to do several t-test within a for loop. Example follows: > > data1<- rnorm(1:25) > data2<- rnorm(1:25) > vars<- c("data1", "data2") > > for (i in vars) { > t.test(i) > } > > Unfortunately, it does not work because of the quotes in the vars-vector (running t.test(data1) by hand works). > > How can I remove the quotes before performing the test?> > Sincerely, > Arne Schulz > Here are two ways: # 1. use get() for (i in vars) { print(t.test(get(i))) } # 2. put your data into a list (or dataframe); then use lapply(); L <- list(data1 = data1, data2 = data2) lapply(L, t.test) -Peter Ehlers