简体   繁体   中英

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?

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' ie '"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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM