r - Extract elements from a list given the positions in another list -
i have list of matrices follow.
listmat <- list(a=matrix(1:9, ncol=3), b=matrix(10:18, ncol=3)) listmat $a [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 $b [,1] [,2] [,3] [1,] 10 13 16 [2,] 11 14 17 [3,] 12 15 18
i want extract 1st , 2nd column of matrix , 3rd column of b. these positions given in list.
listposition <- list(a=c(1,2), b=c(1)) listposition $a [1] 1 2 $b [1] 1
is there way extract elements rather using following command
listmat[["a"]][,1:2] listmat[["b"]][,1]
we can use map
extract corresponding matrix
elements in list
using index in listposition
map(function(x, y) x[, y, drop = false], listmat, listposition)
Comments
Post a Comment