Dear all, I have received some data arranged like this: # original data id <- rep(letters[1:6], each=2) time <- as.factor(rep(1:2, 6)) y <- as.vector(replicate(6, c(rnorm(n=1, mean=1), rnorm(n=1, mean=2)))) test.data <- data.frame(id, time, y) test.data I would like to perform a paired t-test of the y-values at time=1 against those at time=2, with samples paired by their id. Is it necessary to arrange the data in a format like this: # rearranged data id <- letters[1:6] y1 <- replicate(6, rnorm(n=1, mean=1)) # y-value at time = 1 y2 <- replicate(6, rnorm(n=1, mean=2)) # y-value at time = 2 test.data2 <- data.frame(id, y1, y2) test.data2 ...in order to perform a paired t-test? t.test(y1, y2, paired=T) If yes, which is the most convenient way to rearrange the data? Or is it possible to apply the paired t-test function to the original data set? And a side question: In my examples, I suppose can I use set.seed to reproduce the 'rnorm-values' created in the 'original data' also in my the 'rearranged data'. Can someone give me a hint of how to apply the same 'seed' to all the rnorms? Thanks a lot in advance! Henrik
On Sun, 6 Aug 2006, Henrik Parn wrote:> Dear all, > > I have received some data arranged like this: > > # original data > id <- rep(letters[1:6], each=2) > time <- as.factor(rep(1:2, 6)) > y <- as.vector(replicate(6, c(rnorm(n=1, mean=1), rnorm(n=1, mean=2)))) > test.data <- data.frame(id, time, y) > test.data > > I would like to perform a paired t-test of the y-values at time=1 > against those at time=2, with samples paired by their id. Is it > necessary to arrange the data in a format like this: > > # rearranged data > id <- letters[1:6] > y1 <- replicate(6, rnorm(n=1, mean=1)) # y-value at time = 1Really, rnorm(6, 1) suffices here.> y2 <- replicate(6, rnorm(n=1, mean=2)) # y-value at time = 2 > test.data2 <- data.frame(id, y1, y2) > test.data2 > > ...in order to perform a paired t-test? > t.test(y1, y2, paired=T) > > If yes, which is the most convenient way to rearrange the data? > Or is it possible to apply the paired t-test function to the original > data set?t.test(y ~ time, test.data, paired=TRUE)> And a side question: In my examples, I suppose can I use set.seed to > reproduce the 'rnorm-values' created in the 'original data' also in my > the 'rearranged data'. Can someone give me a hint of how to apply the > same 'seed' to all the rnorms?Using the same seed will give identical values from rnorm, surely not what you want. You need to generate the rnorms in the same order, that is all. matrix(rnorm(12) + c(1,2), 6, 2, byrow=TRUE) will do the trick. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hi one possibility is t(unstack(test.data, y~id)) HTH Petr On 6 Aug 2006 at 16:13, Henrik Parn wrote: Date sent: Sun, 06 Aug 2006 16:13:29 +0200 From: Henrik Parn <henrik.parn at bio.ntnu.no> Organization: NTNU To: R-help <r-help at stat.math.ethz.ch> Subject: [R] paired t-test. Need to rearrange data? Send reply to: henrik.parn at bio.ntnu.no <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe> <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>> Dear all, > > I have received some data arranged like this: > > # original data > id <- rep(letters[1:6], each=2) > time <- as.factor(rep(1:2, 6)) > y <- as.vector(replicate(6, c(rnorm(n=1, mean=1), rnorm(n=1, > mean=2)))) test.data <- data.frame(id, time, y) test.data > > I would like to perform a paired t-test of the y-values at time=1 > against those at time=2, with samples paired by their id. Is it > necessary to arrange the data in a format like this: > > # rearranged data > id <- letters[1:6] > y1 <- replicate(6, rnorm(n=1, mean=1)) # y-value at time = 1 > y2 <- replicate(6, rnorm(n=1, mean=2)) # y-value at time = 2 > test.data2 <- data.frame(id, y1, y2) > test.data2 > > ...in order to perform a paired t-test? > t.test(y1, y2, paired=T) > > If yes, which is the most convenient way to rearrange the data? > Or is it possible to apply the paired t-test function to the original > data set? > > And a side question: In my examples, I suppose can I use set.seed to > reproduce the 'rnorm-values' created in the 'original data' also in my > the 'rearranged data'. Can someone give me a hint of how to apply the > same 'seed' to all the rnorms? > > > Thanks a lot in advance! > > > Henrik > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz