R removing extra rows in a matrix compared with another matrix -


i'll illustrate question example:

a1 #     x y #adam 1 #mike 2 b #mary 3 c  a2 #     j #adam 4 e #mary 5 f 

and want this:

a3    x y #adam 1 #mary 3 c 

where values in a1 don't change, , rows don't appear in a2 removed (by rowname only). searched quite while , none of packages, such compare or data.frame worked me.

i'm totally new r, , working on quite large dataset, please me solution runs quick :)

thanks!

posting comment answer:

a3 = a1[rownames(a1) %in% rownames(a2), ] 

this pretty simple. @ row names a1 in a2, , keep rows. should work whether data in matrix or data.frame.

as commented on other answer, viewed inner join operation. can inner join base::merge, or if need speed data.table or dplyr packages. use need convert matrices data frames , add row names own column. if data in matrix, using rownames() method show above efficient enough.


Comments