a/b/c is different variables, t1 is time point 1, t2 is time point 2, t3 is time point 3.
The purpose is to create a two new columns: one with the first and one with the last non missing value for each row of a_t1 to a_t3. On the condition that it is also non missing in the variables b and c (at the same time point).
I think coalesce()
could work with some kind of conditional formatting. However, my knowledge of this is limited. Preferably tidyverse solution, but other solutions works as well.
library(tidyverse)
df<-tibble::tribble(
~a_t1, ~a_t2, ~a_t3, ~b_t1, ~b_t2, ~b_t3, ~c_t1, ~c_t2, ~c_t3,
1, NA, 9, 2, NA, 6, 3, NA, 7,
2, NA, 8, NA, 5, 8, NA, 1, 8,
NA, NA, 3, 2, NA, 9, 2, NA, 22,
NA, 5, 9, 4, NA, 9, 4, 5, NA,
NA, 9, 10, NA, 6, 11, NA, 6, NA
)
Expected output for the first value of a: 1, 8, 3, NA, 9
Expected output for the last value of a: 9, 8, 3, NA, 9
(in the real dataset there is more timepoints and variables to consider)