dre
dre

Reputation: 494

r: replace_na() not replacing NAs

I have a variable with NAs called df$salesContribution.

Using dplyr I've created a statement below, but can't figure out why my df$salesContribution is returning NAs still:

df<- df %>% 
  mutate(salesContribution = as.numeric(salesContribution)) %>% 
  replace_na(0)

Is the 0 not registering?

Upvotes: 1

Views: 4390

Answers (5)

Jeffrey Harding
Jeffrey Harding

Reputation: 60

I found a blog response elsewhere in which Hadley himself said they wanted to move away from replace_na toward a more SQL adjacent command coalesce(). The solution involves both across and coalesce. In my case, I used a different variable from the dataframe to supply the missing values ad hoc. You can also specify a fixed value.

Here's an example of what I just did in my work:

Varname1 Varname2
1 Yes Yes
2 NA No
3 NA Yes
4 No No
df %>%
  mutate(across(Varname1, coalesce, Varname2))
Varname1 Varname2
1 Yes Yes
2 No No
3 Yes Yes
4 No No

Upvotes: 1

DFM
DFM

Reputation: 31

replace_na() will not work if the variable is a factor, and the replacement is not already a level for your factor. If this is the issue, you can add another level to your factor variable for 0 before running replace_na(), or you can convert the variable to numeric or character first.

Upvotes: 3

Vivek Sinha
Vivek Sinha

Reputation: 197

If using data.table package you could do something like: You could try something like: x[is.na(field_name)][, field_name := replacement_value]

There should be similar syntax for data.frame as well.

Upvotes: 0

Julius Vainora
Julius Vainora

Reputation: 48191

It looks like you want

df$salesContribution <- df$salesContribution %>% as.numeric() %>% replace_na(0)

Upvotes: 1

YOLO
YOLO

Reputation: 21709

You can do using base replace:

df<- df %>% 
  mutate(salesContribution = replace(as.numeric(salesContribution), which(is.na(salesContribution)), 0)

Upvotes: 0

Related Questions