R Question
I am looking to join multiple data frames of unequal size. While joining the data frames, I would like to overwrite any NAs. I attempted to use the coalesce function, but equal sized data frames were required.
Example
x <- data.frame(
ID = c(1,2,3,4,5),
Location = c("Georgia", NA, NA, "Idaho", "Texas"),
Cost = c(NA, 200, NA, 400, 500)
)
y <- data.frame(
ID = c(1, 2, 3),
Location = c("Wyoming", "Florida", "Toronto"),
Cost = c(150, 100, 450)
)
Desired Result
ID Location Cost
1 Georgia 150
2 Florida 200
3 Toronto 450
4 Idaho 400
5 Texas 500
You can do a
full_join
and then usecoalesce
forLocation
andCost
columns.In base R, we can use
ifelse
to select values fromLocation
andCost
columns.