【问题标题】:How to use R to create baseball Splits from Game Logs如何使用 R 从游戏日志中创建棒球拆分
【发布时间】:2021-05-29 15:59:24
【问题描述】:

我正在尝试使用 R 重新创建 MLB.com 上的棒球拆分。拆分是从游戏日志创建的,并提供不同的数据切割。例如,主场与客场比赛、白天与夜间比赛、八月与九月等等,都在一张方便的桌子上。我相信,一旦基本拆分总计,所有比率(AVG、OBP SLG)都可以通过 mutate 添加。

我的问题是,创建这些拆分的最佳和最有效的方法是什么,以及应该如何塑造数据。游戏日志显然有包含拆分主题的附加(隐藏)列。问题的性质使我相信 purrr 可能是一种可以使用的工具,但我无法完全确定如何解决这个问题。

这是我认为应该如何塑造数据的方式以及link to a sample game log。对于这个问题的任何想法、想法或解决方案,我将不胜感激。

国家外野手胡安·索托的比赛日志和比赛记录的链接和图片如下所示。

游戏日志:Juan Soto Game Log

拆分:Juan Soto Game Splits

拆分

【问题讨论】:

    标签: r dplyr tidyverse


    【解决方案1】:

    我已经浏览了数据集,但我不确定 sum 的值是否匹配,也不确定与上面图片相关的平均值。

    为了创造你所建议的价值观,你是对的。

    但是,希望我的方法可以帮助你得到你想要的。

    library(tidyverse)
    library(data.table)
    
    game.splits <- "https://raw.githubusercontent.com/MundyMSDS/GAMELOG/main/SAMPLE_GAME_LOG.csv"
    game.splits <- fread(game.splits, fill = TRUE)
    
    game.splits.pivot <- game.splits
    game.splits.pivot$Var1 <- ifelse(game.splits.pivot$Var1 %in% "HOME", 1, 0)
    game.splits.pivot$Var2 <- ifelse(game.splits.pivot$Var2 %in% "NIGHT", 3, 2)
    game.splits.pivot$Var3 <- ifelse(game.splits.pivot$Var3 %in% "SEPTEMBER", 5, 4)
    
    game.splits.pivot <- game.splits.pivot %>% pivot_longer(-c(1:16, 20))
    colnames(game.splits.pivot)[19] <- "name_c"
    game.splits.pivot <- game.splits.pivot[, -c(17, 18)]
    game.splits.pivot <- game.splits.pivot %>% pivot_longer(-c(1:3, 17))
    
    #test
    game.splits.pivot_test <- game.splits.pivot[, -c(1, 2, 3)]
    game.splits.pivot_test <- aggregate(value ~ name_c + name, game.splits.pivot_test, sum)
    game.splits.pivot_test <- game.splits.pivot_test %>% pivot_wider(names_from = name, values_from = value)
    
    lc_name <- tibble(name_c = 0:5, split = c("HOME", "AWAY", "DAY", "NIGHT", "AUGUST", "SEPTEMBER"))
    
    game.splits.pivot_test <- game.splits.pivot_test %>% 
      inner_join(lc_name, by = "name_c") %>% 
      arrange(name_c) %>% 
      select(-name_c)
    
    game.splits.pivot_test <- game.splits.pivot_test[, c(14, 3, 9, 6, 1, 2, 7, 10, 4, 8, 12, 11, 5, 13)]
    

    查看数据集:

    # A tibble: 6 x 14
      split        AB     R     H  `2B`  `3B`    HR   RBI    BB   IBB    SO    SB    CS    TB
      <chr>     <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
    1 HOME         88    24    32     5     0     9    23    15     5    12     1     2    64
    2 AWAY         66    15    22     9     0     4    14    26     7    16     5     0    43
    3 DAY          29    21    18     4     0     5    17    12     4     3     4     0    37
    4 NIGHT       125    18    36    10     0     8    20    29     8    25     2     2    70
    5 AUGUST       90    21    33     6     0    11    25    13     1    13     1     1    72
    6 SEPTEMBER    64    18    21     8     0     2    12    28    11    15     5     1    35
    

    【讨论】:

      【解决方案2】:

      事实证明,这比我想象的更直接。以下解决方案依赖 pivot_longer 来塑造数据并 summarise_if 来计算拆分 - 不需要 rbinds 或 purrr。

      library(tidyverse)
              
              game.splits <- "https://raw.githubusercontent.com/MundyMSDS/GAMELOG/main/SAMPLE_GAME_LOG.csv"
              game.splits <- read_csv(game.splits)
              
              
              game.splits %>% 
                pivot_longer(Var1:Var3, names_to = "split") %>% 
                group_by(split) %>% 
                arrange(split) %>% 
                select(split, value, everything()) %>% 
                ungroup() %>% 
                select(split, value, everything()) %>% 
                select(-Date, -OPP) %>% 
                mutate(value = str_c(split, "_", value)) %>% 
                group_by(value) %>% 
                summarise_if(is.numeric, sum) %>% 
                mutate(value= str_replace(value, "(Var\\d_)",""))
          
          
              #> # A tibble: 6 x 14
              #>   value     AB     R     H    TB  `2B`  `3B`    HR   RBI    BB   IBB    SO    SB
              #>   <chr>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
              #> 1 AWAY      88    24    32    64     5     0     9    23    15     5    12     1
              #> 2 HOME      66    15    22    43     9     0     4    14    26     7    16     5
              #> 3 DAY       29    21    18    37     4     0     5    17    12     4     3     4
              #> 4 NIGHT    125    18    36    70    10     0     8    20    29     8    25     2
              #> 5 AUGUST    90    21    33    72     6     0    11    25    13     1    13     1
              #> 6 SEPTE~    64    18    21    35     8     0     2    12    28    11    15     5
          
          Created on 2021-03-03 by the reprex package (v0.3.0)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多