0

I'm a novice in R and need a simple method for this task. I have a first matrix A

    [,1] [,2] 
[1,] 1  "a"
[2,] 2  "b"
[3,] 3  "c"
[4,] 4  "d"
[5,] 5  "e"
[6,] 6  "f"
[7,] 7  "g"
[8,] 8  "h"

second matrix B

     [,1] [,2] 
[1,] 3  "t"
[2,] 5  "p"
[3,] 7  "n"
[4,] 8  "k"
[5,] 9  "mm"

I need to create matrix C

     [,1] [,2] [,3]
[1,] 3    "c"  "t" 
[2,] 5    "e"  "p"
[3,] 7    "g"  "n"
[4,] 8    "h"  "k"

I need to find common elements in first column in A and B matrix and create third matrix C with this common elements in first column, elements from matrix A in second column and elements from matrix B in third column.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 2
    As a side note, storing different column types in a matrix is a bad idea usually. – David Arenburg Jan 21 '16 at 15:12
  • Use `merge`. But since you have matrices, specify the key columns for the match like `merge(m1, m2, by="V1")`. You do not have column names, but "V1" is the default column name given to your objects after they are changed to data frames internally by the function. You should also consider using data frames instead of matrices. – Pierre L Jan 21 '16 at 15:19

0 Answers0