Home > OS >  Updating by reference a data.table using another, but only empty values
Updating by reference a data.table using another, but only empty values

Time:09-07

I am trying to update some dt1 data.table using another dt2 data.table on common id for a specific column. That specific column should be updated only if the value of the specific column in dt1 is empty (NA).

For instance, this updates everything :

dt1 <- data.table(
  "id" = c(1,2,3,4),
  "animal" = c("cat","cat","dog",NA)
)
dt2 <- data.table(
  "id" = c(3,4),
  "animal" = c("human being","duck")
)


dt1 <- dt1[dt2, on = c("id"),"animal" := .(i.animal)]

Which gives for dt1

id   animal
1:  1      cat
2:  2      cat
3:  3      human being
4:  4      duck

While I need

id   animal
1:  1      cat
2:  2      cat
3:  3      dog
4:  4     duck

CodePudding user response:

We could use fcoalecse

library(data.table)
dt1[dt2, animal := fcoalesce(animal, i.animal), on = .(id)]

-output

> dt1
      id animal
   <num> <char>
1:     1    cat
2:     2    cat
3:     3    dog
4:     4   duck
  • Related