-2

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.

HappyCoding
  • 5,029
  • 7
  • 31
  • 51

1 Answers1

0

You can use a join (dplyr):

library(dplyr)

A <- data.frame(id = c(1, 2, 3), value = rep(NA, 3))
B <- data.frame(id = c(3, 2), value = c(3, 2))

A %>% left_join(B, by='id') %>%
  mutate(value = ifelse(is.na(value.x),value.y,value.x))

see comment of your question to learn what joining data is all about.

Wietze314
  • 5,942
  • 2
  • 21
  • 40