I have a problem with combining data frames which share the same rows. I imported rasters from different folders and converted them into data frames and tried to combine them to
I'm not sure if you need more than just these 4 dataframes, but I've put the main idea here for you. You/we can adjust for more than these 4, or when the dataframes are in a list, etc...
Data:
dput(df1)
structure(list(x = c(696060L, 696090L, 696120L, 696150L, 696180L,
696210L, 696240L, 696270L, 696300L, 696330L), y = c(-3327450L,
-3327450L, -3327450L, -3327450L, -3327450L, -3327450L, -3327450L,
-3327450L, -3327450L, -3327450L), R1 = c(66L, 71L, 69L, 65L,
67L, 68L, 67L, 68L, 69L, 0L)), class = "data.frame", row.names = c(NA,
-10L))
dput(df2)
structure(list(x = c(696060L, 696090L, 696120L, 696150L, 696180L,
696210L, 696240L, 696270L, 696300L, 696330L), y = c(-3327450L,
-3327450L, -3327450L, -3327450L, -3327450L, -3327450L, -3327450L,
-3327450L, -3327450L, -3327450L), R2 = c(66L, 71L, 69L, 65L,
67L, 68L, 67L, 68L, 69L, 0L)), class = "data.frame", row.names = c(NA,
-10L))
dput(df3)
structure(list(x = c(753810L, 753840L, 753870L, 753900L, 753930L,
753960L, 753990L, 754020L, 754050L, 754080L), y = c(-3339930L,
-3339930L, -3339930L, -3339930L, -3339930L, -3339930L, -3339930L,
-3339930L, -3339930L, -3339930L), R1 = c(109L, 108L, 108L, 109L,
108L, 109L, 109L, 109L, 110L, 109L)), class = "data.frame", row.names = c(NA,
-10L))
dput(df4)
structure(list(x = c(753810L, 753840L, 753870L, 753900L, 753930L,
753960L, 753990L, 754020L, 754050L, 754080L), y = c(-3339930L,
-3339930L, -3339930L, -3339930L, -3339930L, -3339930L, -3339930L,
-3339930L, -3339930L, -3339930L), R2 = c(109L, 108L, 108L, 109L,
108L, 109L, 109L, 109L, 110L, 109L)), class = "data.frame", row.names = c(NA,
-10L))
First combine per 2 dataframes by using dplyr
s left_join
:
combined1 <- left_join(df1, df2, by = c("x" = "x", "y" = "y"))
combined1
x y R1 R2
1 696060 -3327450 66 66
2 696090 -3327450 71 71
3 696120 -3327450 69 69
4 696150 -3327450 65 65
5 696180 -3327450 67 67
6 696210 -3327450 68 68
7 696240 -3327450 67 67
8 696270 -3327450 68 68
9 696300 -3327450 69 69
10 696330 -3327450 0 0
combined2 <- left_join(df3, df4, by = c("x" = "x", "y" = "y"))
combined2
x y R1 R2
1 753810 -3339930 109 109
2 753840 -3339930 108 108
3 753870 -3339930 108 108
4 753900 -3339930 109 109
5 753930 -3339930 108 108
6 753960 -3339930 109 109
7 753990 -3339930 109 109
8 754020 -3339930 109 109
9 754050 -3339930 110 110
10 754080 -3339930 109 109
Then you can rbind
these into the full dataframe you want:
allCombined <- rbind(combined1, combined2)
allCombined
x y R1 R2
1 696060 -3327450 66 66
2 696090 -3327450 71 71
3 696120 -3327450 69 69
4 696150 -3327450 65 65
5 696180 -3327450 67 67
6 696210 -3327450 68 68
7 696240 -3327450 67 67
8 696270 -3327450 68 68
9 696300 -3327450 69 69
10 696330 -3327450 0 0
11 753810 -3339930 109 109
12 753840 -3339930 108 108
13 753870 -3339930 108 108
14 753900 -3339930 109 109
15 753930 -3339930 108 108
16 753960 -3339930 109 109
17 753990 -3339930 109 109
18 754020 -3339930 109 109
19 754050 -3339930 110 110
20 754080 -3339930 109 109
Does this make sense?