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

Rui Barradas ruipbarradas at sapo.pt
Fri May 4 21:30:26 CEST 2012


Hello,


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
> 
> 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.
> 

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


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



More information about the R-help mailing list