NukeDude
NukeDude

Reputation: 27

Unexpected behavior with case_when and is.na

I want to change all NA values in a column to 0 and all other values to 1. However, I can't get the combination of case_when and is.na to work.

# Create dataframe
a <- c(rep(NA, 9), 2, rep(NA, 10))
b <- c(rep(NA, 9), "test", rep(NA, 10))
df <- data.frame(a, b, stringsAsFactors = FALSE)

# Create new column (c), where all NA values in (a) are transformed to 0 and other values are transformed to 1
df <- df %>% 
  mutate(
    c = case_when(
      a == is.na(.$a) ~ 0,
      FALSE ~ 1
    )
  )

I expect column (c) to indicate all 0 values and one 1 value, but it's all 0's.

It does work when I use an if_else statement with is.na, like:

df <- df %>% 
  mutate(
    c = if_else(is.na(a), 0, 1))
  )

What is going on here?

Upvotes: 0

Views: 1636

Answers (1)

joran
joran

Reputation: 173547

You should be doing this instead:

df %>% 
    mutate(
        c = case_when(
            is.na(a) ~ 0,
            TRUE ~ 1
        )
    )

Upvotes: 2

Related Questions