[R] sample and rearrange

David Winsemius dwinsemius at comcast.net
Thu May 20 03:17:22 CEST 2010


On May 19, 2010, at 7:47 PM, Wu Gong wrote:

>
> I tried to use a separate function to make the code more  
> understandable. But
> I failed. I don't know what's wrong with the code.
>
> x <- as.matrix(x)
>
> rearrange <- function(.row){
> 	z <- do.call(rbind, strsplit(.row[-1], ''))
> 	z.col <- t(apply(z, 2, paste, collapse=''))
> 	cbind(.row[1], z.col)
> 	}
> 	
> t(apply(x, 1, rearrange(.row)))
>
> Error in strsplit(.row[-1], "") : object '.row' not found

The error occurs because apply is sending a single row at a time, but  
it is not named .row. Your code _does_ work, but only if you use it  
thusly:

t(apply(x, 1, rearrange))


>
> I don't know how to pass the value to the function.

You may not, ... but R knows how.
>
> -----
> A R learner.
> -- 
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list