2

I am trying to merge a list of matrices all by the first column like this:

a  x   x
a  q   q
b  y   y
c  z   z
d  w   w               x  x  x  x
e  v   v               q  q  q  q
e  r   r               y  y  y  y
          ---------->  z  z  z  z    
a  x   x               w  w  w  w   
a  q   q               v  v  v  v
b  y   y               r  r  r  r
c  z   z
d  w   w           
e  v   v
e  r   r     

I would like to use the first column to combine the matrices but it does not need to be in the resulting matrix. The thing that is challenging me is the fact that there are multiple instances of the same value in the first row (a and e)

I have been looking around but unable to find any solutions that account for the same values in the column that the matrices are being joined with. With my current code (shown bellow) I get something like:

x  x  x  x
q  q  q  q
x  x  x  x
q  q  q  q
x  x  x  x
q  q  q  q
y  y  y  y
z  z  z  z    
w  w  w  w   
v  v  v  v
r  r  r  r
v  v  v  v
r  r  r  r
v  v  v  v
r  r  r  r

I cant seem to find out why the duplicate rows are appearing but it has something to do with the length of list so I am assuming it takes place in the merge function.

mergeM <- function(list){ # list is a list of matrices
   len = length(list)   

  mat = merge(list[[1]],list[[2]],by.x = "V1", by.y = "V1", all = TRUE)
  if(len >2){
     for(i in 3:len){
       mat = merge(mat,list[[i]],by.x = "V1", by.y = "V1", all = TRUE)
     }
  }
  mat = mat[,-1]
  return(mat)
}# end function
CU_dev
  • 243
  • 1
  • 14
  • This question might be useful: https://stackoverflow.com/questions/37749412/select-only-the-first-row-when-merging-data-frames-with-multiple-matches – Joris C. Jul 01 '19 at 18:27

0 Answers0