Here it is in function form: > # DF is data frame > # k is number of levels > # ids is a vector of column numbers of id variables > > read.reps <- function(DF, k = 2, ids = 1) { + n <- prod(dim(DF[-ids])) / k # no of rows of output + m <- ncol(DF[-ids])/k # no of reps per line + data.frame(id = DF[gl(n, m, n), ids], t(matrix(t(DF[-ids]), k))) + } > > # read in test data > Lines <- "A 2.5 3.4 2.7 5.6 5.7 5.4 10.1 9.4 + B 5.3 5.4 6.5 7.5 1.3 4.5 10.5 4.1" > DF <- read.table(textConnection(Lines))
> # test 1 > read.reps(DF) id X1 X2 1 A 2.5 3.4 2 A 2.7 5.6 3 A 5.7 5.4 4 A 10.1 9.4 5 B 5.3 5.4 6 B 6.5 7.5 7 B 1.3 4.5 8 B 10.5 4.1 > # test 2 > read.reps(DF, 4) id X1 X2 X3 X4 1 A 2.5 3.4 2.7 5.6 2 A 5.7 5.4 10.1 9.4 3 B 5.3 5.4 6.5 7.5 4 B 1.3 4.5 10.5 4.1 > # test 3 > read.reps(DF, 3, 1:3) id.V1 id.V2 id.V3 X1 X2 X3 1 A 2.5 3.4 2.7 5.6 5.7 1.1 A 2.5 3.4 5.4 10.1 9.4 2 B 5.3 5.4 6.5 7.5 1.3 2.1 B 5.3 5.4 4.5 10.5 4.1 On Sun, Apr 11, 2010 at 2:17 PM, RockO <rock.oui...@gmail.com> wrote: > > Gabor's elegant method works as well indeed where gl(number of factor > level,number of replications): > >> data.frame(id = dat[gl(3,4),1], t(matrix(t(dat[-1]), 2))) > > Rock > DRF-MRNF, Quebec > -- > View this message in context: > http://n4.nabble.com/Read-data-in-sequences-tp1819487p1836285.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > ______________________________________________ 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.