0

In df1, I would like to have only the same ID rows as in df2. How can I select rows that are in df2 based on ID and drop the other rows?

     df1             
     ID Var1    Var2    Col1    Col2
     1    34      22      34      24
     2     3      25      54      65
     3    87      68      14      78
     4    66      98      98     100
     5    55      13      77       2

     df2             
     ID Varx    VarY    ColZ    Col2
     1    134      2      4      4
     2     33      5      4      5
     4    660      8      8      100




     Expected outcome:
     df1             
     ID Var1    Var2    Col1    Col2
     1    34      22      34      24
     2     3      25      54      65
     4    66      98      98     100
AlexP
  • 147
  • 2
  • 9

1 Answers1

1

This should work:

df1[df1$ID %in% df2$ID, ]
neilfws
  • 32,751
  • 5
  • 50
  • 63