【发布时间】:2020-03-29 19:06:00
【问题描述】:
我正在尝试将 NFL 球队与他们各自的城市联系起来。在尝试 case_when 我得到 NA 时。下面是代码
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"
)
)
输出
team_name state
"Lions" NA
"Lions" NA
"Lions" NA
"Lions" NA
"Lions" NA
"Packers" NA
另外,有没有其他方法可以做到这一点?
【问题讨论】:
-
请提供可重现的数据集。