Two input datasets:
A <- data.frame(id = c(1, 2, 3), value = rep(NA, 3))
A
id value
<dbl> <lgl>
1 1 NA
2 2 NA
3 3 NA
B <- data.frame(id = c(3, 2), value = c(3, 2))
B
id value
1 3 3
2 2 2
After adding in available value in B to A, it's expected to have:
A
id value
<dbl> <lgl>
1 1 NA
2 2 2
3 3 3
It can be achieved with following for loop. However, for-loop is in general very slow. How to do it more efficiently?
for(i in 1:nrow(A)){
item <- A[i,]
print(item)
if(is.na(item$value) && (item$id %in% B$id)){
A[i, "value"] <- B[B$id == item$id,]$value
}
}
Join can solve this problem. but requiring a rule to resolve the conflict.