Why:
data.frame(a=c(TRUE,NA)) %>% mutate(a=replace_na(FALSE))
returns
a
1 FALSE
2 FALSE
and so set the entire column to the value instead of just the NA elements?
In this case mutate
is supposed to call the "vector" version of replace_na
, that is, it should be equivalent to:
> df <- data.frame(a=c(TRUE,NA))
> df$a <- replace_na(df$a, FALSE)
The docs for replace_na(data, replace)
says:
If data is a vector, replace takes a single value. This single value replaces all of the missing values in the vector. replace will be cast to the type of data.
On a side note, and not related to this question,
data.frame(a=c(TRUE,NA)) %>% replace_na(list("a"=FALSE))
works as expected.
You are misusing mutate() by not giving the replace_na() function a column to operate on. Running replace_na(FALSE) simple returns FALSE and that explains your result. The third line of code in the reprex gives the result you want.
library(tidyverse)
data.frame(a=c(TRUE,NA)) %>% mutate(a= replace_na(FALSE))
#> a
#> 1 FALSE
#> 2 FALSE
replace_na(FALSE)
#> [1] FALSE
data.frame(a=c(TRUE,NA)) %>% mutate(a= replace_na(a,FALSE))
#> a
#> 1 TRUE
#> 2 FALSE
Created on 2024-11-13 with reprex v2.1.1