I thought this should be as simple task, but somehow it seems to be more complicated than expected.
What I want is the following:
- I have a data frame with a character column
- This data frame also contains a numeric column where the characters are mapped to a certain numbers
- I want to append/join/update this numeric column based on an external lookup table for those cases in my data frame where the numeric column is NA.
Example:
x <- data.frame(string = c("A", "B", "C", "D", "D"),
a1 = c(1, 2, 3, NA, NA))
y <- data.frame(string = c("D", "E", "F"),
a1 = c("4", "5", "6"))
I tried:
library(tidyverse)
x %>%
rows_update(., y, by = "string")
But this throws an error:
Error:
x
key values are not unique.
Expected outcome:
string a1
A 1
B 2
C 3
D 4
D 4
So how could I fill in an existing column based on a a lookup table - preferably in the tidyverse?