|
I have the following:
Time A1 A1 B1 B1 C1 C2 x y x y x y 0 5 6 6 7 7 9 1 3 4 4 3 9 9 2 5 2 6 4 7 4 I want to change it to the following: 0 1 2 x y x y x y A1 5 6 3 4 5 2 B1 6 7 4 3 6 4 etc for a much larger set I am sure there are ways to accomplish through a lot of for loops but I feel there is most likely a function to use. Thank you. |
|
On Thu, May 03, 2012 at 07:36:45PM -0700, marc212 wrote:
> I have the following: > Time A1 A1 B1 B1 C1 C2 > x y x y x y > 0 5 6 6 7 7 9 > 1 3 4 4 3 9 9 > 2 5 2 6 4 7 4 > > I want to change it to the following: > 0 1 2 > x y x y x y > A1 5 6 3 4 5 2 > B1 6 7 4 3 6 4 > etc for a much larger set Hi. Try the following. # the example input Orig <- rbind( "0"=c(5, 6, 6, 7, 7, 9), "1"=c(3, 4, 4, 3, 9, 9), "2"=c(5, 2, 6, 4, 7, 4)) colnames(Orig) <- rep(c("x", "y"), times=3) # transformation Arr <- array(Orig, dim=c(nrow(Orig), 2, ncol(Orig)/2)) Arr , , 1 # this is the required row 1 in the output [,1] [,2] [1,] 5 6 [2,] 3 4 [3,] 5 2 , , 2 # this is the required row 2 in the output [,1] [,2] [1,] 6 7 [2,] 4 3 [3,] 6 4 , , 3 [,1] [,2] [1,] 7 9 [2,] 9 9 [3,] 7 4 Arr1 <- aperm(Arr, perm=c(2, 1, 3)) d <- dim(Arr1) t(array(Arr1, dim=c(d[1]*d[2], d[3]))) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 5 6 3 4 5 2 [2,] 6 7 4 3 6 4 [3,] 7 9 9 9 7 4 Hope this helps. Petr Savicky. ______________________________________________ [hidden email] 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. |
|
I have something like 28 rows and 6000 columns. How would I configure this with for loops.
Thank you. Marc |
|
In reply to this post by marc212
Hello,
Using Petr's Orig data.frame above, maybe this is what you want. cnames <- unique(colnames(Orig)) nc <- ncol(Orig)/length(cnames) res <- lapply(seq.int(nrow(Orig)), function(i){ x <- Orig[i, ] dim(x) <- c(length(cnames), nc) dimnames(x) <- list(cnames, LETTERS[seq_len(nc)]) t(x) }) names(res) <- rownames(Orig) res do.call(cbind, res) And it avoids loops. Hope this helps, Rui Barradas |
|
I am not opposed to for loops just do not know how to implement them.
I am sorry for all the questions I am trying to learn. The posted code does not work for me in my set I am getting a : Error in `dimnames<-.data.frame`(`*tmp*`, value = list(c("A1", "A1.1", : invalid 'dimnames' given for data frame Any help is much appreciated. Thank you. |
|
In reply to this post by marc212
On Fri, May 04, 2012 at 11:11:47AM -0700, marc212 wrote:
> I have something like 28 rows and 6000 columns. How would I configure this > with for loops. The transformation may be splitted into simpler pieces using a for loop over the 28 rows. Try the following. orig <- rbind( "0"=c(5, 6, 6, 7, 7, 9), "1"=c(3, 4, 4, 3, 9, 9), "2"=c(5, 2, 6, 4, 7, 4)) colnames(orig) <- rep(c("x", "y"), times=3) out <- matrix(nrow=ncol(orig)/2, ncol=2*nrow(orig)) for (i in seq.int(length=nrow(orig))) { out[, 2*(i-1) + 1:2] <- matrix(orig[i, ], nrow=nrow(out), ncol=2, byrow=TRUE) } out [,1] [,2] [,3] [,4] [,5] [,6] [1,] 5 6 3 4 5 2 [2,] 6 7 4 3 6 4 [3,] 7 9 9 9 7 4 Hope this helps. Petr Savicky. ______________________________________________ [hidden email] 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. |
|
In reply to this post by marc212
Hello,
Try commenting out the line dimnames <- list(... etc ...) You could also use dput() to post an example of your data. (Small example.) Rui Barradas |
|
This post has NOT been accepted by the mailing list yet.
This post was updated on .
I have a pretty good computer with a lot of RAM and it just wont run this code. My R crashes...
Running this on a sample does not work either. Any advice? Thank you for all the help! Marc |
|
Hello, again.
What I'm saying is to use dput on a data sample. For instance, with 'orig' created as above, dput(orig) structure(c(5, 3, 5, 6, 4, 2, 6, 4, 6, 7, 3, 4, 7, 9, 7, 9, 9, 4), .Dim = c(3L, 6L), .Dimnames = list(c("0", "1", "2"), c("x", "y", "x", "y", "x", "y"))) If you post an example like this, we'll know better what to do. It's just a matter of copy&paste to an R session. Rui Barradas |
|
This post was updated on .
Short snippet-
structure(list(A2 = structure(c(9L, 4L, 4L, 3L, 5L, 7L, 5L, 7L, 6L, 1L, 1L, 1L, 3L, 4L, 5L, 5L, 2L, 5L, 3L, 4L, 4L, 8L, 4L, 3L, 4L, 5L, 4L, 3L), .Label = c("4.957", "4.958", "4.959", "4.96", "4.961", "4.962", "4.963", "4.964", "x"), class = "factor"), A2.1 = structure(c(6L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 4L, 2L, 4L, 2L, 5L, 4L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("6.156", "6.157", "6.158", "6.159", "6.161", "y"), class = "factor"), A3 = structure(c(9L, 2L, 2L, 5L, 5L, 5L, 5L, 4L, 5L, 3L, 6L, 1L, 8L, 2L, 7L, 2L, 2L, 6L, 3L, 1L, 2L, 2L, 2L, 5L, 3L, 4L, 8L, 3L), .Label = c("5.114", "5.115", "5.116", "5.117", "5.118", "5.119", "5.12", "5.121", "x"), class = "factor"), A3.1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("4.227", "y"), class = "factor"), A4 = structure(c(14L, 5L, 5L, 3L, 7L, 6L, 7L, 1L, 5L, 4L, 2L, 9L, 5L, 12L, 7L, 11L, 11L, 4L, 11L, 3L, 8L, 6L, 10L, 6L, 9L, 3L, 13L, 3L), .Label = c("5.204", "5.207", "5.209", "5.21", "5.211", "5.212", "5.213", "5.214", "5.215", "5.216", "5.218", "5.219", "5.221", "x"), class = "factor"), A4.1 = structure(c(9L, 4L, 4L, 2L, 6L, 8L, 5L, 6L, 1L, 4L, 4L, 2L, 6L, 3L, 2L, 4L, 4L, 6L, 4L, 8L, 4L, 3L, 6L, 4L, 2L, 7L, 3L, 6L), .Label = c("2.564", "2.565", "2.566", "2.567", "2.569", "2.57", "2.571", "2.572", "y"), class = "factor"), B1 = structure(c(8L, 4L, 4L, 3L, 3L, 5L, 7L, 5L, 3L, 4L, 2L, 4L, 2L, 3L, 4L, 4L, 5L, 4L, 4L, 6L, 4L, 2L, 2L, 5L, 5L, 4L, 4L, 1L), .Label = c("7.273", "7.274", "7.275", "7.276", "7.277", "7.278", "7.279", "x"), class = "factor"), B1.1 = structure(c(8L, 1L, 1L, 5L, 3L, 4L, 4L, 2L, 3L, 4L, 3L, 3L, 4L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 4L, 7L, 4L, 3L, 5L, 5L, 6L), .Label = c("8.067", "8.068", "8.069", "8.07", "8.071", "8.072", "8.073", "y"), class = "factor"), A1 = structure(c(6L, 5L, 5L, 3L, 3L, 1L, 4L, 4L, 3L, 1L, 2L, 3L, 2L, 1L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 3L, 5L), .Label = c("4.918", "4.919", "4.92", "4.921", "4.922", "x"), class = "factor"), A1.1 = structure(c(6L, 3L, 3L, 2L, 4L, 5L, 3L, 1L, 3L, 3L, 3L, 3L, 5L, 5L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 1L, 3L, 4L), .Label = c("8.297", "8.298", "8.299", "8.3", "8.301", "y"), class = "factor"), B2 = structure(c(10L, 5L, 5L, 5L, 7L, 7L, 7L, 9L, 8L, 5L, 2L, 8L, 5L, 4L, 2L, 6L, 3L, 2L, 9L, 5L, 4L, 2L, 5L, 5L, 1L, 8L, 9L, 5L), .Label = c("7.272", "7.273", "7.274", "7.275", "7.276", "7.277", "7.278", "7.279", "7.28", "x"), class = "factor"), B2.1 = structure(c(6L, 4L, 4L, 3L, 3L, 2L, 2L, 2L, 4L, 3L, 4L, 3L, 2L, 4L, 3L, 4L, 4L, 5L, 1L, 4L, 3L, 4L, 4L, 4L, 5L, 3L, 3L, 5L), .Label = c("6.056", "6.058", "6.059", "6.06", "6.061", "y"), class = "factor"), B3 = structure(c(10L, 1L, 1L, 4L, 5L, 2L, 4L, 7L, 7L, 7L, 2L, 3L, 7L, 4L, 4L, 9L, 8L, 7L, 6L, 7L, 5L, 4L, 6L, 9L, 7L, 8L, 6L, 4L), .Label = c("7.411", "7.412", "7.413", "7.414", "7.415", "7.416", "7.417", "7.418", "7.419", "x"), class = "factor"), B3.1 = structure(c(7L, 3L, 3L, 3L, 5L, 4L, 4L, 2L, 5L, 3L, 4L, 4L, 4L, 3L, 1L, 3L, 4L, 3L, 3L, 4L, 2L, 3L, 4L, 6L, 4L, 3L, 4L, 3L), .Label = c("4.05", "4.052", "4.053", "4.054", "4.055", "4.056", "y"), class = "factor"), B4 = structure(c(10L, 2L, 2L, 3L, 4L, 5L, 9L, 8L, 4L, 4L, 6L, 4L, 3L, 5L, 4L, 5L, 8L, 7L, 4L, 4L, 4L, 6L, 4L, 6L, 2L, 3L, 1L, 3L), .Label = c("7.468", "7.469", "7.47", "7.471", "7.472", "7.473", "7.474", "7.475", "7.476", "x"), class = "factor"), B4.1 = structure(c(6L, 4L, 4L, 3L, 4L, 4L, 1L, 3L, 4L, 5L, 4L, 2L, 3L, 4L, 3L, 5L, 3L, 3L, 4L, 4L, 5L, 4L, 4L, 4L, 5L, 2L, 5L, 4L), .Label = c("2.274", "2.275", "2.276", "2.277", "2.278", "y"), class = "factor"), C1 = structure(c(6L, 4L, 4L, 4L, 4L, 5L, 3L, 3L, 3L, 1L, 2L, 2L, 4L, 2L, 4L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 3L, 3L, 1L), .Label = c("9.744", "9.745", "9.746", "9.747", "9.748", "x"), class = "factor"), C1.1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("8.203", "y"), class = "factor"), C2 = structure(c(9L, 5L, 5L, 4L, 3L, 5L, 6L, 6L, 7L, 6L, 4L, 8L, 8L, 6L, 8L, 5L, 1L, 3L, 4L, 1L, 3L, 5L, 5L, 6L, 8L, 5L, 2L, 4L), .Label = c("9.916", "9.917", "9.918", "9.919", "9.92", "9.921", "9.922", "9.923", "x"), class = "factor"), C2.1 = structure(c(4L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 1L), .Label = c("5.942", "5.943", "5.944", "y"), class = "factor"), C3 = structure(c(10L, 8L, 8L, 8L, 9L, 8L, 2L, 5L, 5L, 6L, 5L, 5L, 4L, 7L, 4L, 5L, 1L, 6L, 6L, 3L, 2L, 2L, 1L, 2L, 3L, 7L, 5L, 4L), .Label = c("9.95", "9.951", "9.952", "9.953", "9.954", "9.955", "9.956", "9.959", "9.96", "x"), class = "factor"), C3.1 = structure(c(7L, 6L, 6L, 5L, 6L, 5L, 5L, 4L, 4L, 4L, 2L, 4L, 6L, 6L, 6L, 6L, 5L, 1L, 3L, 6L, 6L, 5L, 5L, 6L, 6L, 5L, 6L, 6L), .Label = c("3.952", "3.953", "3.954", "3.955", "3.956", "3.957", "y"), class = "factor"), C4 = structure(c(10L, 6L, 6L, 4L, 5L, 3L, 4L, 6L, 7L, 1L, 8L, 4L, 4L, 4L, 6L, 7L, 9L, 5L, 8L, 7L, 8L, 5L, 8L, 2L, 5L, 1L, 2L, 1L), .Label = c("9.914", "9.915", "9.916", "9.917", "9.918", "9.919", "9.92", "9.921", "9.922", "x"), class = "factor"), C4.1 = structure(c(10L, 2L, 3L, 1L, 8L, 3L, 3L, 5L, 5L, 4L, 7L, 8L, 8L, 8L, 7L, 9L, 8L, 7L, 7L, 6L, 9L, 9L, 8L, 8L, 8L, 8L, 9L, 8L), .Label = c("2.242", "2.244", "2.245", "2.246", "2.247", "2.248", "2.249", "2.25", "2.252", "y"), class = "factor"), D1 = structure(c(7L, 4L, 6L, 3L, 6L, 5L, 4L, 4L, 6L, 2L, 2L, 3L, 2L, 2L, 3L, 6L, 6L, 2L, 6L, 5L, 2L, 5L, 6L, 6L, 6L, 1L, 3L, 4L), .Label = c("12.452", "12.453", "12.454", "12.455", "12.456", "12.457", "x"), class = "factor"), D1.1 = structure(c(5L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 2L, 3L, 2L, 3L, 4L, 2L, 4L, 1L, 2L, 2L, 4L, 4L, 3L, 3L, 2L, 1L, 2L, 4L, 1L), .Label = c("8.491", "8.492", "8.493", "8.494", "y"), class = "factor"), D2 = structure(c(7L, 3L, 3L, 3L, 4L, 3L, 5L, 4L, 3L, 5L, 3L, 3L, 3L, 3L, 1L, 4L, 3L, 5L, 4L, 5L, 2L, 4L, 3L, 4L, 3L, 6L, 5L, 2L), .Label = c("12.794", "12.795", "12.796", "12.797", "12.798", "12.802", "x"), class = "factor"), D2.1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6.273", "y"), class = "factor"), D3 = structure(c(5L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 1L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L), .Label = c("12.775", "12.776", "12.777", "12.778", "x"), class = "factor"), D3.1 = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("4.034", "y"), class = "factor"), D4 = structure(c(8L, 2L, 3L, 2L, 4L, 1L, 3L, 4L, 3L, 4L, 4L, 3L, 3L, 5L, 4L, 5L, 5L, 5L, 5L, 4L, 6L, 5L, 7L, 5L, 5L, 4L, 5L, 6L), .Label = c("12.499", "12.501", "12.502", "12.503", "12.504", "12.505", "12.506", "x"), class = "factor"), D4.1 = structure(c(8L, 5L, 1L, 6L, 6L, 2L, 6L, 6L, 7L, 5L, 6L, 5L, 2L, 6L, 4L, 7L, 2L, 2L, 4L, 1L, 5L, 4L, 5L, 5L, 4L, 1L, 2L, 3L), .Label = c("2.142", "2.143", "2.144", "2.145", "2.146", "2.147", "2.148", "y" ), class = "factor"), E2 = structure(c(8L, 1L, 1L, 2L, 3L, 4L, 2L, 4L, 4L, 3L, 3L, 5L, 4L, 4L, 5L, 3L, 4L, 3L, 3L, 3L, 6L, 4L, 3L, 4L, 6L, 6L, 7L, 4L), .Label = c("17.01", "17.013", "17.014", "17.015", "17.016", "17.017", "17.018", "x"), class = "factor"), E2.1 = structure(c(6L, 5L, 1L, 1L, 4L, 2L, 1L, 4L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6.813", "6.814", "6.815", "6.816", "6.819", "y"), class = "factor"), E3 = structure(c(6L, 5L, 5L, 3L, 2L, 3L, 4L, 2L, 1L, 4L, 3L, 2L, 5L, 3L, 1L, 2L, 3L, 2L, 2L, 3L, 1L, 3L, 3L, 2L, 3L, 3L, 1L, 2L), .Label = c("17.061", "17.062", "17.063", "17.064", "17.065", "x"), class = "factor"), E3.1 = structure(c(9L, 1L, 2L, 3L, 3L, 6L, 2L, 3L, 6L, 7L, 4L, 7L, 4L, 4L, 4L, 6L, 8L, 5L, 6L, 4L, 5L, 8L, 7L, 7L, 7L, 4L, 6L, 7L), .Label = c("4.265", "4.266", "4.267", "4.268", "4.269", "4.27", "4.271", "4.272", "y"), class = "factor"), E4 = structure(c(7L, 3L, 4L, 4L, 4L, 5L, 6L, 5L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 4L, 2L, 5L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 6L), .Label = c("17.443", "17.444", "17.445", "17.446", "17.447", "17.448", "x"), class = "factor"), E4.1 = structure(c(7L, 6L, 5L, 3L, 3L, 4L, 2L, 1L, 3L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L), .Label = c("2.357", "2.358", "2.359", "2.36", "2.362", "2.364", "y"), class = "factor"), F1 = structure(c(13L, 9L, 6L, 9L, 12L, 11L, 7L, 3L, 10L, 5L, 5L, 6L, 7L, 9L, 3L, 9L, 5L, 8L, 1L, 3L, 3L, 8L, 2L, 6L, 4L, 2L, 3L, 3L), .Label = c("19.828", "19.829", "19.83", "19.831", "19.832", "19.833", "19.834", "19.835", "19.836", "19.837", "19.838", "19.839", "x"), class = "factor"), F1.1 = structure(c(9L, 1L, 5L, 1L, 1L, 3L, 3L, 4L, 2L, 3L, 1L, 3L, 4L, 3L, 4L, 7L, 6L, 3L, 5L, 6L, 6L, 6L, 8L, 7L, 8L, 6L, 7L, 6L), .Label = c("8.775", "8.776", "8.777", "8.778", "8.779", "8.78", "8.781", "8.782", "y"), class = "factor"), F3 = structure(c(6L, 5L, 5L, 2L, 3L, 5L, 5L, 3L, 1L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 4L, 3L, 5L, 4L, 3L, 4L, 4L, 4L, 3L, 2L, 3L, 5L), .Label = c("19.521", "19.522", "19.523", "19.524", "19.525", "x"), class = "factor"), F3.1 = structure(c(10L, 6L, 8L, 6L, 5L, 9L, 7L, 5L, 5L, 7L, 6L, 5L, 6L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 2L, 3L, 2L, 3L, 1L, 4L, 5L, 3L), .Label = c("4.227", "4.228", "4.229", "4.23", "4.231", "4.232", "4.233", "4.234", "4.236", "y"), class = "factor"), F2 = structure(c(7L, 3L, 5L, 6L, 4L, 4L, 4L, 4L, 3L, 3L, 2L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 1L, 3L, 3L, 3L, 3L, 1L, 2L, 3L, 6L, 5L), .Label = c("19.491", "19.492", "19.493", "19.494", "19.495", "19.496", "x"), class = "factor"), F2.1 = structure(c(5L, 4L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 2L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("6.423", "6.425", "6.426", "6.427", "y"), class = "factor"), F4 = structure(c(6L, 4L, 2L, 2L, 3L, 3L, 1L, 2L, 3L, 2L, 5L, 2L, 1L, 3L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 4L, 1L, 3L, 2L, 3L), .Label = c("19.573", "19.574", "19.575", "19.576", "19.577", "x"), class = "factor"), F4.1 = structure(c(6L, 5L, 3L, 1L, 4L, 2L, 3L, 1L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, 3L, 1L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 1L), .Label = c("2.514", "2.515", "2.516", "2.517", "2.521", "y"), class = "factor"), G1 = structure(c(9L, 5L, 5L, 6L, 5L, 5L, 4L, 2L, 5L, 4L, 6L, 7L, 3L, 1L, 3L, 4L, 6L, 5L, 6L, 6L, 4L, 3L, 2L, 3L, 5L, 3L, 8L, 5L), .Label = c("21.994", "21.999", "22", "22.001", "22.002", "22.003", "22.004", "22.006", "x"), class = "factor"), G1.1 = structure(c(10L, 9L, 7L, 5L, 8L, 7L, 6L, 8L, 5L, 7L, 4L, 5L, 7L, 6L, 4L, 7L, 8L, 3L, 2L, 8L, 5L, 6L, 5L, 4L, 6L, 3L, 5L, 1L), .Label = c("8.713", "8.715", "8.717", "8.718", "8.719", "8.72", "8.721", "8.722", "8.724", "y"), class = "factor"), G2 = structure(c(9L, 6L, 6L, 4L, 4L, 4L, 5L, 6L, 4L, 3L, 1L, 4L, 5L, 4L, 3L, 4L, 4L, 4L, 5L, 5L, 3L, 4L, 5L, 5L, 2L, 7L, 8L, 6L), .Label = c("21.936", "21.937", "21.938", "21.939", "21.94", "21.941", "21.942", "21.943", "x"), class = "factor"), G2.1 = structure(c(5L, 4L, 1L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("6.388", "6.389", "6.391", "6.396", "y"), class = "factor")), .Names = c("A2", "A2.1", "A3", "A3.1", "A4", "A4.1", "B1", "B1.1", "A1", "A1.1", "B2", "B2.1", "B3", "B3.1", "B4", "B4.1", "C1", "C1.1", "C2", "C2.1", "C3", "C3.1", "C4", "C4.1", "D1", "D1.1", "D2", "D2.1", "D3", "D3.1", "D4", "D4.1", "E2", "E2.1", "E3", "E3.1", "E4", "E4.1", "F1", "F1.1", "F3", "F3.1", "F2", "F2.1", "F4", "F4.1", "G1", "G1.1", "G2", "G2.1"), class = "data.frame", row.names = c(NA, -28L)) > |
|
Ok, I think I've got it this time.
The problem was that you have two different types of data in the same data structure, the first row are the result's column names, then the actual numeric data. First, in what follows I've called your data.frame 'df1', df1 <- structure(list(A2 = structure(c(9L, 4L, ...etc... Now the code. dat <- apply(df1[-1, ], 2, as.numeric) nr <- nrow(dat) nc <- ncol(dat) names1 <- colnames(df1)[rep(c(TRUE, FALSE), nc/2)] names2 <- unique(unname(apply(df1, 2, function(x) as.character(x[1])))) res <- matrix(nrow=nc/2, ncol=2) inx <- as.matrix(rev(expand.grid(1:2, 1:(nc/2)))) res <- do.call(cbind, lapply(seq.int(nr), function(i){res[inx] <- dat[i, ]; matrix(res, ncol=2)})) res <- data.frame(res) rownames(res) <- names1 colnames(res) <- paste(names2, rep(seq.int(nr), each=2), sep=".") res I hope this is, finally, it. Rui Barradas
|
| Powered by Nabble | Edit this page |
