Summarizing Question

Hello...I was wondering if someone can help me summarize my data by player_name & pitch and show/count all the different events that happens. Thank you!

bdat <- data.frame(
   player_name = as.factor(c("bob", "bob", "bob", "bob", "bob", "bob", "bob",
                             "bob", "bob", "bob", "billy", "billy", "billy",
                             "billy", "billy", "tom", "tom", "tom", "tom", "tom",
                             "aaron", "bob", "bob", "bob")),
         event = as.factor(c("ball", "calledstrike", "ball", "calledstrike",
                             "foul", "foul", "foul", "foul", "foul",
                             "calledstrike", "strikeswinging", "ball", "strikeswinging",
                             "ball", "calledstrike", "strikeswinging",
                             "calledstrike", "foul", "foul", "strikeswinging", "bunt",
                             "calledstrike", "foul", "strikeswinging")),
         pitch = as.factor(c("curveball", "fastball", "slider", "fastball",
                             "fastball", "fastball", "curveball", "fastball",
                             "curveball", "fastball", "curveball", "curveball",
                             "curveball", "fastball", "fastball", "fastball",
                             "fastball", "fastball", "curveball", "fastball",
                             "fastball", "curveball", "fastball", "curveball"))
)

This is a typical use of the group_by() and summarize() functions of the dplyr package. The n() function returns a count of the rows.

bdat <- data.frame(
  player_name = as.factor(c("bob", "bob", "bob", "bob", "bob", "bob", "bob",
                            "bob", "bob", "bob", "billy", "billy", "billy",
                            "billy", "billy", "tom", "tom", "tom", "tom", "tom",
                            "aaron", "bob", "bob", "bob")),
  event = as.factor(c("ball", "calledstrike", "ball", "calledstrike",
                      "foul", "foul", "foul", "foul", "foul",
                      "calledstrike", "strikeswinging", "ball", "strikeswinging",
                      "ball", "calledstrike", "strikeswinging",
                      "calledstrike", "foul", "foul", "strikeswinging", "bunt",
                      "calledstrike", "foul", "strikeswinging")),
  pitch = as.factor(c("curveball", "fastball", "slider", "fastball",
                      "fastball", "fastball", "curveball", "fastball",
                      "curveball", "fastball", "curveball", "curveball",
                      "curveball", "fastball", "fastball", "fastball",
                      "fastball", "fastball", "curveball", "fastball",
                      "fastball", "curveball", "fastball", "curveball"))
)

library(dplyr)

Counts <- bdat %>% group_by(player_name, pitch) %>% summarize(N = n())
Counts
#> # A tibble: 8 x 3
#> # Groups:   player_name [?]
#>   player_name pitch         N
#>   <fct>       <fct>     <int>
#> 1 aaron       fastball      1
#> 2 billy       curveball     3
#> 3 billy       fastball      2
#> 4 bob         curveball     5
#> 5 bob         fastball      7
#> 6 bob         slider        1
#> 7 tom         curveball     1
#> 8 tom         fastball      4

Created on 2019-09-17 by the reprex package (v0.2.1)

2 Likes

This is what I understand from your request

library(dplyr)

bdat %>% 
    group_by(player_name, pitch) %>% 
    count(event)
#> # A tibble: 16 x 4
#> # Groups:   player_name, pitch [8]
#>    player_name pitch     event              n
#>    <fct>       <fct>     <fct>          <int>
#>  1 aaron       fastball  bunt               1
#>  2 billy       curveball ball               1
#>  3 billy       curveball strikeswinging     2
#>  4 billy       fastball  ball               1
#>  5 billy       fastball  calledstrike       1
#>  6 bob         curveball ball               1
#>  7 bob         curveball calledstrike       1
#>  8 bob         curveball foul               2
#>  9 bob         curveball strikeswinging     1
#> 10 bob         fastball  calledstrike       3
#> 11 bob         fastball  foul               4
#> 12 bob         slider    ball               1
#> 13 tom         curveball foul               1
#> 14 tom         fastball  calledstrike       1
#> 15 tom         fastball  foul               1
#> 16 tom         fastball  strikeswinging     2
2 Likes

Thanks for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.