0

I have two data sets

1 10
2 15
3 17
4 5

The second

1 to
2 b
4 c

I need to compare only one column of the two sets and at the end have what values are equal to the two sets in that column and exclude the rest, in summary when comparing the two columns I would have the following result

1 10
2 15
4 5

I don't know where to start, if someone can help me get started

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Try `subset(df1, col1 %in% df2$col1)` – akrun May 17 '20 at 21:54
  • 2
    But be careful about types. If you type `c(1, "to", 3 ,"b")`, R interprets that as `[1] "1" "to" "3" "b" `. (Character data) But `c(1, 10, 2, 15, 3, 17, 4, 5)` becomes `[1] 1 10 2 15 3 17 4 5` (Numeric data). In your data, `1` won't match `"1"` and `2` won't match `"2"`. You either need to get those non-numerics out of `col1` or coerce `col2` to character. – David T May 17 '20 at 22:06

1 Answers1

1

We can use subset from base R (if the first column name in both datasets are 'col1')

subset(df1, col1 %in% df2$col1)
akrun
  • 874,273
  • 37
  • 540
  • 662