Canovice
Canovice

Reputation: 10173

Is it possible to combine summarise with summarise_at in a single group_by with dplyr

Edit: just realized the side column in the data isn't used at all, so please disregard it for the purposes of the example.

I have a large dataframe of play-by-play basketball data, and I would like to perform a group_by, summarise and summarise_at on my data. Below is a subset of my dataframe:

> dput(zed)
structure(list(side = c("right", "right", "right", "right", "right", 
"right", "left", "right", "right", "right", "left", "right", 
"left", "left", "left", "right", "right", "right", "left", "right"
), result = c("twopointmiss", "twopointmade", "twopointmade", 
"twopointmiss", "twopointmade", "twopointmade", "twopointmiss", 
"twopointmade", "twopointmade", "twopointmade", "twopointmade", 
"twopointmade", "twopointmiss", "twopointmiss", "twopointmiss", 
"twopointmiss", "twopointmade", "twopointmade", "twopointmiss", 
"twopointmiss"), zonenumber = c(1, 1, 1, 1, 2, 3, 2, 3, 2, 3, 
4, 4, 4, 1, 1, 2, 3, 2, 3, 4), team = c("Bos", "Bos", "Bos", 
"Bos", "Bos", "Bos", "Bos", "Bos", "Bos", "Bos", "Min", "Min", 
"Min", "Min", "Min", "Min", "Min", "Min", "Min", "Min")), row.names = c(3L, 
5L, 8L, 14L, 17L, 23L, 28L, 30L, 39L, 41L, 42L, 43L, 47L, 52L, 
54L, 58L, 60L, 63L, 69L, 72L), class = "data.frame")

>   zed
    side       result zonenumber team
3  right twopointmiss          1  Bos
5  right twopointmade          1  Bos
8  right twopointmade          1  Bos
14 right twopointmiss          1  Bos
17 right twopointmade          2  Bos
23 right twopointmade          3  Bos
28  left twopointmiss          2  Bos
30 right twopointmade          3  Bos
39 right twopointmade          2  Bos
41 right twopointmade          3  Bos
42  left twopointmade          4  Min
43 right twopointmade          4  Min
47  left twopointmiss          4  Min
52  left twopointmiss          1  Min
54  left twopointmiss          1  Min
58 right twopointmiss          2  Min
60 right twopointmade          3  Min
63 right twopointmade          2  Min
69  left twopointmiss          3  Min
72 right twopointmiss          4  Min

In the example below, i only use summarise, as I'm currently not sure how to use summarise and summarise_at with the same group_by call:

>   grouped.df <- zed %>%
+     dplyr::group_by(team) %>%
+     dplyr::summarise(
+       shotsMade = sum(result == "twopointmade"),
+       shotsAtt = n(),
+       shotsPct = round(shotsMade / shotsAtt),
+       points = 2 * shotsMade,
+       
+       z1Made = sum(zonenumber == 1),
+       z2Made = sum(zonenumber == 2),
+       z3Made = sum(zonenumber == 3),
+       z4Made = sum(zonenumber == 4)
+     )
>   grouped.df
# A tibble: 2 x 9
  team  shotsMade shotsAtt shotsPct points z1Made z2Made z3Made z4Made
  <chr>     <int>    <int>    <dbl>  <dbl>  <int>  <int>  <int>  <int>
1 Bos           7       10        1     14      4      3      3      0
2 Min           4       10        0      8      2      2      2      4

In the example below, I'd like to create the first 4 columns (shotsMade, shotsAtt, shotsPct, points) in summarise, and create the z#made columns with a summarise_at. In my full data, there are ~30 unique-ish columns that I plan on creating with summarise, and ~80 similar-ish columns that I plan on creating with summarise_at.

For sake of a small example, I didn't want to bring my entire dataframe in for this example. If I am able to implement both summarise and summarise_at in the example above, then I'll be able to do it for my full data frame as well.

Any thoughts on this is greatly appreciated, as I am particularly keen on improving with the _at functions in dplyr. Thanks!

Upvotes: 1

Views: 1077

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

I don't think there is a way to actually use both summarise and summarise_at as clearly we wouldn't be able to execute the second one after losing many rows and columns.

So, instead we may use mutate, mutate_at, and then drop certain rows (and perhaps columns).The difference between this and somehow magically applying summarise and summarise_at is going to be that the former approach will not drop any variables. I guess it depends whether that's a good thing for you. Below I add an extra line of select(-one_of(setdiff(names(zed), "team"))) that will actually drop all the columns that the summarise combo would drop.

zed$zonenumber2 <- zed$zonenumber # Example
zed %>%
  group_by(team) %>%
  mutate(
    shotsMade = sum(result == "twopointmade"),
    shotsAtt = n(),
    shotsPct = round(shotsMade / shotsAtt),
    points = 2 * shotsMade) %>%
  mutate_at(
    vars(contains("zone")), 
    .funs = funs(Made1 = sum(. == 1), Made2 = sum(. == 2),
                 Made3 = sum(. == 3), Made4 = sum(. == 4))) %>%
  filter(!duplicated(team)) %>%
  select(-one_of(setdiff(names(zed), "team"))) # May want to remove
# A tibble: 2 x 13
# Groups:   team [2]
#   team  shotsMade shotsAtt shotsPct points zonenumber_Made1 zonenumber2_Mad… zonenumber_Made2
#   <chr>     <int>    <int>    <dbl>  <dbl>            <int>            <int>            <int>
# 1 Bos           7       10        1     14                4                4                3
# 2 Min           4       10        0      8                2                2                2
# … with 5 more variables: zonenumber2_Made2 <int>, zonenumber_Made3 <int>,
#   zonenumber2_Made3 <int>, zonenumber_Made4 <int>, zonenumber2_Made4 <int>

Upvotes: 3

Related Questions