robertspierre
robertspierre

Reputation: 4431

Why replace_na with mutate doesn't work as expected?

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.

Upvotes: 0

Views: 62

Answers (2)

Yong Wang
Yong Wang

Reputation: 1313

To solve it, the usage of replace_na example is followed:

library(tidyr)
library(dplyr)

data.frame(a=c(TRUE,NA)) %>%
  mutate(a=replace_na(a,
                      FALSE)
         )

according to replace_na document, https://tidyr.tidyverse.org/reference/replace_na.html. it should be

replace_na(data, replace, ...)

in your original code: replace_na(FALSE) result is FALSE

however, your desired answer is to replace df$a na to FALSE, so the right mode should be :

replace_na(df$a, FALSE).

in dplyr mutate function and %>% pipeline mode, the df$a could be write as a. the the tidy dplyr way is :

mutate(a=replace_na(a,FALSE))

Upvotes: 1

FJCC
FJCC

Reputation: 646

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

Upvotes: 1

Related Questions