-1

I've got two datasets with different numbers of observations that I need to merge. The First one is like this:

state1 state2
1       2
2       3
3       4
4       1

The second one is like this:

state A  state B  distance
1        1       0
1        2       1
1        3       1
1        4       2
2        1       1
2        2       0
2        3       2
2        4       2
3        1       1
3        2       2
3        3       0
3        4       3
4        1       2
4        2       2
4        3       3
4        4       0
...      ...     ...

I would like to combine the distance column of the second dataset to the first one like this:

state1 state2 distance
1       2     1
2       3     2
3       4     3
4       1     2

Is there a way to add the distance info into the first dataset based on the info from the second dataset in R? Thanks.

1 Answers1

0

We can use dplyr::left_join

library(dplyr)

left_join(df1, df2, by = c('state 1' = 'state A', 'state 2' = 'state B'))
GuedesBF
  • 8,409
  • 5
  • 19
  • 37