I am looking to merge two data frames. Both tables have a "Name" column, and I wish to use this column as the one to connect the two. However, when I run something like:
final <- merge(df1, df2, by = "Name")
All of the columns are merged, but I only get the data from df1. And if I run something like:
final <- merge(df1, df2, by = "Name", all.y = TRUE)
The merged table just gives all of the data from df2 but excludes data from df1.
Is there a way around this?
#reproducible example
Names <- c("John", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)
df1 <- data.frame(Names, Age)
Names <- c("John", "Bill", "Maria", "Ben", "Tina")
Cars<- c("Ford", "Kia", "VW", "Toyota", "SAAB")
Color<- c("Red", "Green", "Blue", "Orange", "White")
df2<-data.frame(Names, Cars, Color)
final<- merge( df1, df2, by="Names")
#Expected Result
view(final)
Names Age Cars Color
John 23 Ford Red
Bill 41 Kia Green
Maria 32 VW Blue
Ben 58 Toyota Orange
Tina 26 SAAB White
#Actual Result
Names Age Cars Color
John NA Ford Red
Bill NA Kia Green
Maria NA VW Blue
Ben NA Toyota Orange
Tina NA SAAB White
In this example, I would get the data from df2, but df1 values would show NA.
Is there any way around this?