I would like to solve the following problem with R:
I have a data table with values from two labs: lab1
and lab2
. It could be that lab1
or lab2
contains NA
values so then the column "lab3" should contain the non-NA value. If lab1
and lab2
contain NA
s of course lab3
is also NA
, but if lab1
and lab2
contain values, lab3
should apply the value of lab2 (this is the "more important" lab).
Example:
lab1 lab2 lab3
5 7 7 < lab 3 contains the value of lab 2 (because more important)
8 10 10 < lab 3 contains the value of lab 2 (because more important)
NA 3 3 < lab 3 contains the value of lab 2, because lab 1 is NA
9 NA 9 < lab 3 contains the value of lab 1, because lab 2 is NA
NA NA NA < lab 3 contains NA, because lab 1 and lab 2 contain NA
I found the function coalesce (dplyr
) but I was not able to define the primary importance of lab2
(over lab1
).
Thanks for any help!