Home > Back-end >  How to make an ifelse statement ignore NAs?
How to make an ifelse statement ignore NAs?

Time:05-13

Hello I have this code

df <- df %>%
  mutate(prev_PC = ifelse(changed_PC  == "No" & (is.na(prev_PC) | prev_PC == ""), new_PC, prev_PC))

I'm trying to change the previous postal code. Let's say we have old postal codes and we have new postal codes.

The old postal codes are from a smaller dataset that overlaps with a bigger dataset that has new postal codes. In the same dataset as the old ones ones we have a variable that says no or yes if people changed their postal code (the questionnaire about the old postal codes are asked after the questionnare with the new ones).

I want to use this variable to fill in old postal codes with the new postal codes if they did not change their postal codes. However, since there are about a 1000 NA values in the changed_PC variable, it fills the prev_PC with NAs in those cases, and I am left with more NAs in prev_PC than before. How can I change this?

TL;DR: I want the changed_PC == "No" if statement to ignore NAs. So if changed_PC == NA, I just want it to keep the value in prev_PC as if it said "Yes" instead of making the prev_PC into a NA.

Here is an example:

prev_PC <- c(5039, 1402, 3050, NA, NA, NA, NA, NA)
new_PC <- c(5039, 1402, 3050, 3021, 2154, 4853, 1252, 2954)
changed_PC <- c("No", NA, "No", "Yes", NA, NA, "No", "No")

df <- data.frame(prev_PC , new_PC, changed_PC )

What I want:

prev_PC 
5039, 1402, 3050, NA, NA, NA, 1252, 2954

What I get:

prev_PC
5039, NA, 3050, NA, NA, NA, 1252, 2954

CodePudding user response:

First solution looks like:

df %>%
  mutate(prev_PC = case_when(changed_PC == "No" &
                               is.na(prev_PC) ~ new_PC,
                             TRUE ~ prev_PC))

but this is probably better:

df %>% 
  mutate(prev_PC = if_else(is.na(prev_PC) & 
                               changed_PC == "No", new_PC, prev_PC))

Resulting in:

> df %>% 
    print() %>% 
    mutate(prev_PC = if_else(is.na(prev_PC) & 
                                 changed_PC == "No", new_PC, prev_PC))
  prev_PC new_PC changed_PC
1    5039   5039         No
2    1402   1402       <NA>
3    3050   3050         No
4      NA   3021        Yes
5      NA   2154       <NA>
6      NA   4853       <NA>
7      NA   1252         No
8      NA   2954         No
  prev_PC new_PC changed_PC
1    5039   5039         No
2    1402   1402       <NA>
3    3050   3050         No
4      NA   3021        Yes
5      NA   2154       <NA>
6      NA   4853       <NA>
7    1252   1252         No
8    2954   2954         No

(see the before and after)

  • Related