Reputation: 139
I have 3 data frames, which look like this:
Data Frame 1
ColA ColB ColC
1 B1 1-C1
2 B2 1-C2
3 B3 1-C3
4 B4
5 B5
6 B6
7 B7
8 B8
9 B9 1-C9
10 B10
11 B11
12 B12 1-C12
13 B13
14 B14 1-C14
Data Frame 2
ColA ColB ColC
1 B1
2 B2
3 B3 2-C3
4 B4 2-C4
5 B5
6 B6 2-C6
7 B7
8 B8 2-C8
9 B9 2-C9
10 B10
11 B11
12 B12 2-C12
13 B13
14 B14 2-C14
15 B15 2-C15
Data Frame 3
ColA ColB 3-Col3-C
1 B1
2 B2
3 B3 3-C3
4 B4
5 B5
6 B6 3-C6
7 B7
8 B8
9 B9 3-C9
10 B10
11 B11 3-C11
12 B12 3-C12
13 B13 3-C13
14 B14
15 B15 3-C15
16 B16 3-C16
17 B17 3-C17
I want to merge these data frames. The data in the first data frame takes first priority. The second data frames data takes second priority, and the third set's data takes last priority. How would I do so? My desired output is shown below.
Desired data frame:
ColA ColB ColC
1 B1 1-C1
2 B2 1-C2
3 B3 1-C3
4 B4 2-C4
5 B5
6 B6 2-C6
7 B7
8 B8 2-C8
9 B9 1-C9
10 B10
11 B11 3-C11
12 B12 1-C12
13 B13 3-C13
14 B14 1-C14
15 B15 3-C15
16 B16 3-C16
17 B17 3-C17
Upvotes: 1
Views: 100
Reputation: 887831
We could keep the datasets in a list
, then do a full_join
by 'ColA', 'ColB' and coalesce
the 'ColC' columns to return the output
library(tidyverse)
list(df1, df2, df3) %>%
reduce(full_join, by = c('ColA', 'ColB')) %>%
transmute(ColA, ColB, ColC = coalesce(ColC.x, ColC.y, ColC))
NOTE: Based on the OP's comments, the blanks in the data are NA
elements
Upvotes: 2