Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading