I've got a dataframe like this one:
stage1 stage2 stage3 stage4
a NA b c
NA d NA e
NA NA f g
NA NA NA h
Where each column is a stage from a process. What I want to do is to coalesce each column based on the previous columns:
stage1 stage2 stage3 stage4
a a a a
NA d d d
NA NA f f
NA NA NA h
The actual values don't really matter, this could also be a logical dataframe, where each string from the output is TRUE
and each NA is FALSE
.
I've written this function that lets me coalesce through a selection of columns:
coacross <- function(...) {
coalesce(!!!across(...))
}
df <- df %>%
mutate(total_stages = coacross(everything()))
Which basically creates stage4
column of my desired output. Is there any way to iteratively run this, ideally without a for loop? So I can do the same for stage2
and stage3
? Else, is there another way to do this?
Thanks a lot.
Edit:
This works:
for(col in names(df %>% select(-stage1))){
print(col)
df = df %>%
mutate({{col}} := coacross(stage1:{{col}}))
}
But any more elegant solutions are greatly appreciated