887
887

Reputation: 619

How to incorporate NULL values into case_when?

I am working with the following data set.

Name   X      Y 
Mike   3.2    3.1
Joe    4.5    1.7
Eric   6.2    2.2
Stan   NA     4.8

I am making a new column that creates a new column using mutate and case_when. The value in the new column is the midpoint between X and y if X is greater than 5, or 3/4 of the distance between X and Y if X is less than or equal to 5, or 20 if the value of x is null. I've tried some code along these lines but haven't had any luck.

df <- df %>%
    mutate(NewCol = case_when(X >5 ~ (X+Y)/2,
                              X <= 5 ~ ((X+Y)*3)/4,
                              X = "NA" ~ 20)

This is what I ultimately want to end up with:

Name   X      Y       NewCol
Mike   3.2    3.1     2.475
Joe    4.5    1.7     4.65
Eric   6.2    2.2     4.2
Stan   NA     4.8     20

Upvotes: 0

Views: 3775

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

You can use is.na to check for NA values.

library(dplyr)

df %>%
  mutate(NewCol = case_when(is.na(X) ~ 20,
                            X >5 ~ (X+Y)/2,
                            X <= 5 ~ ((X+Y)*3)/4))

#  Name   X   Y NewCol
#1 Mike 3.2 3.1  4.725
#2  Joe 4.5 1.7  4.650
#3 Eric 6.2 2.2  4.200
#4 Stan  NA 4.8 20.000

data

df <- structure(list(Name = c("Mike", "Joe", "Eric", "Stan"), X = c(3.2, 
4.5, 6.2, NA), Y = c(3.1, 1.7, 2.2, 4.8)), 
class = "data.frame", row.names = c(NA, -4L))

Upvotes: 2

Related Questions