0

I want to merge two dataframes in R by column vector name.

The first dataframe would have and ID variable as this:

H01
H02
H03
H05

And the second dataframe would have an ID variable like this:

H01_1
H02_1
H03_1
H04_1

I want to merge the dataframes, keeping only the columns that are present in both dataframes. So in the previous example H04_1 wouldn´t be in the merged dataframe as it wasn´t present in the first one.

I was thinking of changing the names of the variable:

exclusion %>%
  mutate(Subject = str_sub(Subject, 1, 3))

and then merging, but is there any other simpler way?

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 16 '21 at 22:20
  • I reckon your idea is sound. Merging/joining on a substring will be about as easy as it gets. – thelatemail Jun 16 '21 at 22:24
  • I cant imagine a way that is simpler than yours. Remove the `_1` suffix from the second dataframe, then merge – GuedesBF Jun 16 '21 at 22:55

0 Answers0