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]]
What is it that you don't know how to do? Loop over the matrices from
the 2 lists and merge them two by two, for example
AB <- list() ; id <- 1
for (i in 1:length(A)) for (j in 1:length(B)) {
AB[[id]] <- merge(A[[i]],B[[j]],...)
id <- id + 1
}
To better keep track of who's who, you may want to set up A and B as
named lists, and replace id with
paste(names(A)[i],names(B)[j]).
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Heymans, MW
> Sent: Sunday, October 29, 2006 6:35 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] match lists
>
> 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.
>
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.
>
Apparently Analagous Threads
- gee + rcs
- vacancy postdoc computational systems biology - Amsterdam
- how to get row name using the which function
- [Bug 90276] New: [NVE6] nouveau E[ PFIFO][0000:01:00.0] read fault at 0x000a5c0000 [UNSUPPORTED_KIND] from CE2/GR_CE on channel 0x007f329000 [unknown]
- change column name in a function