Home > Back-end >  Merge dataframes with no corresponding ID
Merge dataframes with no corresponding ID

Time:05-31

I have two dataframes, one with ID and a classification result, and a second one with ID corresponding to the classification results of the first dataframe and associated with a second classification output. I am trying to add the second classification output to the first dataframe, based on the correspondance between the first classification and the classification ID of the second dataframe. Since ID aren't corresponding, I can't do a simple merge.

I am not sure I am clear, so I will provide a reproducible example.

set.seed(15)
df1 <- data.frame(c(1:20), c(floor(runif(20, min=1, max=6))))
df1 <- setNames(df1, c("ID", "classif"))
df2 <- data.frame(c(1:6), c(floor(runif(6, min=1, max=3))))
df2 <- setNames(df2, c("ID_classif", "classif2")) 

The first dataframe below shows the ID and the first classification result

> df1
   ID classif
1   1       4
2   2       1
3   3       5
4   4       4
5   5       2
6   6       5
7   7       5
8   8       2
9   9       4
10 10       5

The second dataframe below shows the classification ID and the second classification result.

> df2
  ID_classif classif2
1   1        1
2   2        2
3   3        2
4   4        3
5   5        3
6   6        3

What I am trying to obtain is a dataframe that will look like the one below, which is basically the first dataframe with the second classification added using the correspondance between "classif" and "ID_classif"

> df3
   ID classif classif2
1   1       4        3
2   2       1        1
3   3       5        3
4   4       4        3
5   5       2        2
6   6       5        3
7   7       5        3
8   8       2        2
9   9       4        3
10 10       5        2

CodePudding user response:

A possible solution:

library(tidyverse)

df1 %>% 
  left_join(df2, by = c("classif" = "ID_classif")) 

#>    ID classif classif2
#> 1   1       4        1
#> 2   2       1        2
#> 3   3       5        2
#> 4   4       4        1
#> 5   5       2        2
#> 6   6       5        2
#> 7   7       5        2
#> 8   8       2        2
#> 9   9       4        1
#> 10 10       5        2
#> 11 11       1        2
#> 12 12       4        1
#> 13 13       3        1
#> 14 14       4        1
#> 15 15       5        2
#> 16 16       5        2
#> 17 17       3        1
#> 18 18       5        2
#> 19 19       1        2
#> 20 20       4        1
  • Related