[R] vectors of matrix as iinput to lapply

Petr Savicky savicky at cs.cas.cz
Sun Feb 5 18:12:39 CET 2012


On Sun, Feb 05, 2012 at 06:54:02AM -0800, Alaios wrote:
> Dear all
> I am using lapply (actually mclapply that share the same syntax).
> 
> I want to call the same function that takes as input a vector. My initial data structure is a matrix that I want to cut it to multiple vectors (one vector for every row of the matrix) and then feed that to the function by using mclapply.
> 
> Could you please help me converting the matrices to nrow times vectors.

Hi.

Try the following

  a <- matrix(1:16, nrow=4)
  x <- lapply(apply(a, 1, FUN=list), unlist)
  x

  [[1]]
  [1]  1  5  9 13
  
  [[2]]
  [1]  2  6 10 14
  
  [[3]]
  [1]  3  7 11 15
  
  [[4]]
  [1]  4  8 12 16

Hope this helps.

Petr Savicky.



More information about the R-help mailing list