I have a set of data with duplicates:
x <- tibble(num=c(1,2,3,2,5,5,8), alph=NA)
And separate sources giving their corresponding values.
y <- tibble(num=1:4, alph=LETTERS[1:4])
z <- tibble(num=5:10, alph=LETTERS[5:10])
Normally, one would use this code to update x$num
with data from y
.
x$alph <- y$alph[match(x$num,y$num)]
Doing the same for z
would nonetheless overwrite what was already in place from y
and replace them with NA
s.
How can I code so that data can be cumulatively updated? Using:
x$alph[which(x$num %in% z$num)] <- y$alph[which(z$num %in% x$num)]
doesn't work because of the duplicate.