I hope anyone could give me a hints on which approach should I use. For example I have a data frame as follows:
Zone <- c(1, 1,1,1,2,2,2)
ID <- c(1,2,3,4,5,6,7)
df1 <- data.frame(Zone, ID)
Zone <- c(1, 1,1,2,2,2,2)
X <- c(1,2,3,4,5,6,7)
Y <- c(1,2,3,4,5,6,7)
df2 <- data.frame(Zone,X, Y)
However when I combined them using simple join as shown, it gives wrong output.
df3 <- df1 %>% inner_join(df2, by = "Zone")
I would like to get this possible result:
Zone <- c(1, 1,1,1,2,2,2)
ID <- c(1,2,3,4,5,6,7)
X <- c(1,2,3,2,5,6,7)
Y <- c(1,2,3,2,5,6,7)
df3 <- data.frame(Zone,ID, X, Y)
Thank you for your help or any approach that may work. Thanks
CodePudding user response:
Try using the cbind
function to column-bind the dataframes:
df2 %>% cbind(df1$ID)
Output:
Zone X Y df1$ID
1 1 1 1 1
2 1 2 2 2
3 1 3 3 3
4 2 4 4 4
5 2 5 5 5
6 2 6 6 6
7 2 7 7 7