【问题标题】:How does one evaluate changes in a categorical variable over time using R?如何使用 R 评估分类变量随时间的变化?
【发布时间】:2019-12-31 11:14:36
【问题描述】:

我有一个数据集,其中两支球队在一年一度的比赛中展开对决。这些比赛有两个分区,东部和西部。我想根据前一年比赛的结果来确定某一年的卫冕冠军是谁。我想为两个部门都这样做。

这是我的数据集:

data <- data.frame(
  Team = c("Hot Dogs", "Hamburgers", "Hot Dogs", "Hamburgers", "Hot Dogs",
           "Hamburgers", "Pho", "Ramen", "Pho", "Ramen", "Pho", "Ramen"),
  Division = c("West", "West", "West", "West", "West", "West", "East", "East",
               "East", "East", "East", "East"),
  Year = c("2017", "2017", "2018", "2018", "2019", "2019", "2017", "2017",
           "2018", "2018", "2019", "2019"),
  Score = c("37", "2", "26", "32", "37", "9", "22", "31", "25", "32", "24", "18"))

理想情况下,我会在原始数据中添加一个“结果”列,以指示给定球队是否是进入该比赛的卫冕冠军。像这样的:

data$Result <- c("Initial Champion", "NA", "Champion", "NA", "NA", "Champion", "NA", 
"Initial Champion", "NA", "Champion", "NA", "Champion")

是否有使用 R 的直接方法,特别是在可能的情况下使用 tidyverse 库?

感谢任何建议。提前致谢。

【问题讨论】:

  • 抱歉,您能解释一下想要的结果df吗?
  • 我想要原始数据框,但根据我上面描述的评估添加了结果列。我在最初的帖子中提供了结果列的样子。希望对您有所帮助。

标签: r


【解决方案1】:

首先我们得到一个包含所有冠军的表格,如果它是第一个,则将它们标记为“初始冠军”,将其他标记为“冠军”:

library(dplyr)
X = data %>% 
arrange(Year,desc(Score)) %>% 
group_by(Division) %>% 
filter(!duplicated(Year))%>% 
mutate(result=rep(c("Initial Champion","Champion"),times=c(1,n()-1)))

# A tibble: 6 x 5
# Groups:   Division [2]
  Team       Division Year  Score result          
  <fct>      <fct>    <fct> <fct> <chr>           
1 Hot Dogs   West     2017  37    Initial Champion
2 Ramen      East     2017  31    Initial Champion
3 Hamburgers West     2018  32    Champion        
4 Ramen      East     2018  32    Champion        
5 Hamburgers West     2019  9     Champion        
6 Pho        East     2019  24    Champion

要进入决赛桌,只需:

left_join(data,X)

【讨论】:

    【解决方案2】:

    在以下答案中,我使用 dplyr 来确定初始冠军和冠军,其中初始冠军意味着第一次出现数据的团队是其部门中年度最佳。在以后的几年里,在其部门中得分最高的球队被认为是冠军。

    library(dplyr)
    
    data <- data.frame(
      Team = c("Hot Dogs", "Hamburgers", "Hot Dogs", "Hamburgers", "Hot Dogs",
               "Hamburgers", "Pho", "Ramen", "Pho", "Ramen", "Pho", "Ramen"),
      Division = c("West", "West", "West", "West", "West", "West", "East", "East",
                   "East", "East", "East", "East"),
      Year = c("2017", "2017", "2018", "2018", "2019", "2019", "2017", "2017",
               "2018", "2018", "2019", "2019"),
      Score = c("37", "2", "26", "32", "37", "9", "22", "31", "25", "32", "24", "18"),
      stringsAsFactors = FALSE)
    
    result <-
      data %>%
      group_by(Year, Division) %>% # First we group by each year and division
    
      # For each division/year we get highest score then for the team with this score
      # we consider it champion
      mutate(high_score = as.character(max(as.numeric(Score), na.rm = TRUE)),
             result = ifelse(high_score == Score, "Champion", NA_character_)) %>%
    
      # Now to determine the initial champion we compare it with the first year
      # if the row contains data of the first year in data then it is initial
      mutate(result = 
               ifelse(min(data$Year) == Year & result == "Champion", "Initial Champion", result)) %>%
    
      # Here we drop high_score column because it is not needed in final output
      select(-high_score)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多