[R] Breaking up a Row in R (transpose)

Rui Barradas ruipbarradas at sapo.pt
Sat May 5 01:58:16 CEST 2012


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


marc212 wrote
> 
> 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))
>>
> 


--
View this message in context: http://r.789695.n4.nabble.com/Breaking-up-a-Row-in-R-transpose-tp4607658p4610151.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list