0

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:

Table A

and table B is:

Table B

I would like to have, as a result:

enter image description here

Would anyone the best way to do that?

1

1 Answer 1

1
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.