Parmeet singh anand
Parmeet singh anand

Reputation: 71

Getting NA's when using case_when in R

I am trying to covert NFL teams with their respective city. While trying case_when I am getting NA's. Below is the code

nfl_RM %>% 
  select(team_name) %>% 
  mutate(state = case_when(
                           team_name %in% c("Rams","Chargers","49ers","Raiders") ~ "California", 
                           team_name %in% c("Jaguars", "Dolphins", "Buccaneers") ~ "Florida",
                           team_name %in% c("Ravens","Redskins") ~ "Maryland",
                           team_name == "Cardinals" ~ "Arizona",
                           team_name %in% c("Eagles","Steelers") ~ "Pennsylvania",
                           team_name %in% c("Bengals","Browns") ~ "Georgia",
                           team_name %in% c("Jets","Giants") ~ "New Jersey",
                           team_name %in% c("Cowboys","Texans") ~ "Texas",
                           team_name == "Broncos" ~ "Colorado",
                           team_name == "Falcons" ~ "Georgia",
                           team_name == "Bears" ~ "Illinois",
                           team_name == "Colts" ~ "Indiana",
                           team_name == "Saints" ~ "Louisiana",
                           team_name == "Patriots" ~ "Massachusetts",
                           team_name == "Lions" ~ "Michigan",
                           team_name == "Vikings" ~ "Minnesota",
                           team_name == "Chiefs" ~ "Missouri",
                           team_name == "Bills" ~ "New York",
                           team_name == "North Carolina" ~ "Panthers",
                           team_name == "Titans" ~ "Tennessee",
                           team_name == "Seahawks" ~ "Washington",
                           team_name == "Packers" ~ "Wisconsin"
                           )
         )

Output

team_name state

"Lions"     NA          
"Lions"     NA          
"Lions"     NA          
"Lions"     NA          
"Lions"     NA          
"Packers"   NA

Also, is there any other way to do this?

Upvotes: 1

Views: 91

Answers (2)

lirbuj
lirbuj

Reputation: 11

nfl_RM %>% 
  select(team_name) %>% 
  mutate (state = case_when(
                            team_name == "Seahawks" ~ "Washington",
                            team_name == "Packers" ~ "Wisconsin",
                            TRUE ~ 'Others'
                           )

issue may result when you do not add a TRUE option to your case when statement, on how to deal with options that do not match any texts.

Upvotes: 1

akrun
akrun

Reputation: 887118

It could be a case where there are already NA in the dataset and == returns NA for those or the strings are not matching exactly. In that case, we may need a str_detect to match the substring

From the OP's output data, it could be also that there are quotes around the 'team_name' i.e. '"Lions"' instead of "Lions"

A better option would be to create a key/val dataset and then do a left_join instead of 100's of case_when

library(dplyr)
keyval <- data.frame(team_name = c("Lions", "Bears",  "Falcons"),
       state = c("Michigan", "Illinois", "Georgia"), stringsAsFactors = FALSE)

nfl_RM %>% 
   select(team_name) %>%
   left_join(keyval) 

Upvotes: 2

Related Questions