[R] sample and rearrange

Wu Gong ghowoo at gmail.com
Wed May 19 23:01:02 CEST 2010


It took me a day to make the sense of Jim's code :(

Hope my comments will help.

## Transform data to matrix
x <- as.matrix(x)

## Apply function to each row
## Create a function to rearrange bases
result <- apply(x, 1, function(eachrow){

## Split each gene to bases
## Exclude the fist column which is id
	bases <- strsplit(eachrow[-1], '')
	
## Transform list to matrix
## Because the result of function strsplit is a list
	bases <- do.call(rbind,bases)
	
## Recombine bases by connecting all bases in each column
	recombine <- apply(bases, 2, paste, collapse="")
	
## Add id
## Transpos recombine
	cbind(eachrow[1], t(recombine))
 })

## Transpose the result matrix	
result <- t(result)

-----
A R learner.
-- 
View this message in context: http://r.789695.n4.nabble.com/sample-and-rearrange-tp2222747p2223594.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list