I am trying to use coalesce() in dplyr. This was working previously but I tweaked some aspects of my code (performing a full_join earlier in the code) and it is no longer working. Where I used to get a column replacing missing values in x with the value in y and two coalesced columns removed, I am now only seeing the value for x in the new column and the two columns that are coalesced aren’t being removed.
Each pair of cols that I’m trying to coalesce are essentially mirrors of each other (i.e., where one has a value the other is empty). They are all char type.
I don’t know what’s gone wrong but I’m guessing it’s something that happened as a result of the join.
I am using this code to coalesce:
df <- df %>%
mutate(
A = coalesce(a,b),
B= colaesce(c,d),
C= coalesce(e,f)
)
sample data:
df <- data.frame(a = c(NA, verison_1, NA, version_1, version_1, NA),
b = c(version_2, NA, version_2, NA, NA, version_2),
c = c(NA, verison_1, NA, version_1, version_1, NA),
d = c(version_2, NA, version_2, NA, NA, version_2),
e = c(NA, verison_1, NA, version_1, version_1, NA),
f = c(version_2, NA, version_2, NA, NA, version_2))
desired output:
|A | B | C |
|————-|————|———-|
|version_2 | version_2 | version_2|
|version_1 | version_1 | version_1|
|version_2 | version_2 | version_2|
|version_1 | version_1 | version_1|
|version_1 | version_1 | version_1|
|version_2 | version_2 | version_2|