I am trying in R to update a table A with another table B only when values in table B are not NA. For example, if table A is:
and table B is:
I would like to have, as a result:
Would anyone the best way to do that?
I am trying in R to update a table A with another table B only when values in table B are not NA. For example, if table A is:
and table B is:
I would like to have, as a result:
Would anyone the best way to do that?
library(tidyverse)
A <- tribble(
~ name, ~X1, ~X2, ~X3,
"AX", 1, 1, NA,
"BL", 6, 1, 3,
"CD",NA, 4, 6,
"DA", 4, NA, NA)
B <- tribble(
~ name, ~X1, ~X2, ~X3,
"AX", 4, 5, 6,
"BL", NA, 3, 4,
"DA", NA, 4, 6)
bind_rows(A, B) %>%
group_by(name) %>%
summarise(across(everything(), ~ ifelse(!is.na(last(.)), last(.), first(.))))
name X1 X2 X3
<chr> <dbl> <dbl> <dbl>
1 AX 4 5 6
2 BL 6 3 4
3 CD NA 4 6
4 DA 4 4 6