[R] match lists
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Oct 31 10:30:35 CET 2006
Try this:
A <- list(matrix(c(2,3,3,1,2,2), 3, dimnames = list(NULL, letters[1:2])),
matrix(c(3,3,2,5,1,3), 3, dimnames = list(NULL, letters[3:4])))
B <- list(matrix(c(1:5, 20*(1:5), 10+20*(1:5)), 5,
dimnames = list(NULL, letters[5:7])),
matrix(c(1:5, 10*(1:5), 10+10*(1:5)), 5,
dimnames = list(NULL, letters[8:10])))
lapply(1:2, function(i) lapply(1:2, function(j) B[[i]][A[[i]][,j],]))
On 10/29/06, Heymans, MW <mw.heymans at vumc.nl> wrote:
> Dear list,
>
> I have this problem, please your advice.
>
> I have list A that contains two matrix elements:
> [[1]]
> a b
> [1,] 2 1
> [2,] 3 2
> [3,] 3 2
> [[2]]
> c d
> [1,] 3 5
> [2,] 3 1
> [3,] 2 3
>
> and list B, that also contains 2 matrices:
> [[1]]
> e f g
> [1,] 1 20 30
> [2,] 2 40 50
> [3,] 3 60 70
> [4,] 4 80 90
> [5,] 5 100 110
> [[2]]
> h i j
> [1,] 1 10 20
> [2,] 2 20 30
> [3,] 3 30 40
> [4,] 4 40 50
> [5,] 5 50 60
>
> Now I want to match each column of list A with each row of list B in such a way that element 1 of list A corresponds to element 1 of list B (and element 2 of A with 2 of B).
> So, in total there will be 4 new matrices, 1 for each column of list A. For example, for column a of list A the new matrix will be:
>
> 2 40 50
> 3 60 70
> 3 60 70
>
> thanks,
> Martijn
> VUmc
> Amsterdam
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list