r - Appending list index number to matrix items as a new column -


simple question no doubt. taking starting point:

l = matrix(1:6, ncol=2) lst = list(l, l) 

how can add list index fresh column each matrix? e.g.

[[1]]      [,1] [,2] [,3] [1,]    1    4    1 [2,]    2    5    1 [3,]    3    6    1  [[2]]      [,1] [,2] [,3] [1,]    1    4    2 [2,]    2    5    2 [3,]    3    6    2 

... assuming matrices have varying numbers of rows. i've attempted various permutations of lapply no luck. in advance.

slightly simpler. practically problem involving applying function each element of 2 (or 3, or n) objects in order can given mapply or map solution (thanks, @mnel):

mapply(cbind, lst, seq_along(lst), simplify=false) # ...and map being wrapper mapply no simplification map(cbind, lst, seq_along(lst))  [[1]]      [,1] [,2] [,3] [1,]    1    4    1 [2,]    2    5    1 [3,]    3    6    1  [[2]]      [,1] [,2] [,3] [1,]    1    4    2 [2,]    2    5    2 [3,]    3    6    2 

Comments

Popular posts from this blog

javascript - Unusual behaviour when drawing lots of images onto a large canvas -

how can i manage url using .htaccess in php? -

javascript - Chart.js - setting tooltip z-index -