我目前正在处理NFL球队的数据。基本上我在这里要做的是创建一个标题为Conference
的新列。我正在尝试创建一个if函数,该函数基于Team列中的Team生成会议。我的数据集名为NFL_DATA
,我感兴趣的列是Team
,因此我使用NFL_DATA$Team
。基本上我想说的是NFL_DATA$TEAM == 'Philadelphia Eagles'
或'Dallas Cowboys'
或'New York Giants'
或'Washington Redskins'
那么它将等于NFC东方。然后在会议专栏中,NFC East将出现在这些团队中。我正在努力为所有8个会议做这件事。任何帮助将不胜感激!!
到目前为止,这就是我所拥有的,我确信这是关闭的:
Conference <- function(NFL_DATA$Team) {
if (NFL_DATA$Team == 'Phiadelphia Eagles' | 'Dallas Cowboys' | 'New York Giants' | 'Washington Redskins') y <- "NFC EAST"
}
答案 0 :(得分:1)
这里不需要功能:
east <- c('Phiadelphia Eagles', 'Dallas Cowboys', 'New York Giants', 'Washington Redskins')
transform(NFL_DATA, Conference = ifelse(Team %in% east, "NFC EAST", "something else")
答案 1 :(得分:0)
Dplyr的案例_对于长期的ifelse陈述很方便:
library(dplyr)
NFL_Data <-
NFL_Data %>% mutate("Conference" = case_when(
Team %in% c(
'Phiadelphia Eagles' ,
'Dallas Cowboys' ,
'New York Giants' ,
'Washington Redskins'
) ~ 'NFC EAST',
Team %in% c(
'New England Patriots' ,
'Buffalo Bills' ,
'New York Jets' ,
'Miami Dolphins'
) ~ 'AFC EAST',
TRUE ~ "NFC WEST"
)
您可以继续在Team %in% c("List_of_Teams") ~ "Conference_Name"
之前为剩余的NFL会议添加TRUE ~ "NFC WEST"
行。