when I create a column with walrus operator ":=" I need to reference the same column later on to replace_na() values.
When I use defuse operators, replace_na() argument doesn't work.
Any suggestions on how to get it to work?
library(tidyverse)
test_fn <- function(column_name){
dat1 <- tibble(
id=1:5
,val=11:15
)
dat2 <- tibble(
id=1:4
,"{{column_name}}":=11:14
)
out <- left_join(
dat1
,dat2
,by=join_by(id)
) |>
replace_na(list("{{column_name}}"=0))
return(out)
}
test_fn(hi)
The intended output is that the "hi" column has no NA values (they are replaced by 0)
The syntax for replace_na
is a little different, so we could use:
... |> mutate({{column_name}} := coalesce({{column_name}}, 0))
or
... |> mutate(across({{column_name}}, ~replace_na(., 0)))