0

I have a df with columns "home team name", "away team name" and game data for each game, I am then creating 2 seperate df for home and away stats for each team. When using for the new df ..

new_df <- df %>%
  group_by(home_team_name) %>%
  summarize (stat1 = mean(stat1), games played = ???)

I am trying to get how many games each team played as a home team and as an away team.

Thanks

jprice
  • 5
  • 2
  • does `n()` work? –  Nov 17 '21 at 21:59
  • 1
    Perhaps `games_played = n()` ? Potential duplicate of https://stackoverflow.com/questions/25869378/what-does-n-n-mean-in-r – jared_mamrot Nov 17 '21 at 21:59
  • @jprice You might want to edit your post and provide information on what your starting data.frame looks like, as well as what your final result should look like. It might make more sense to reshape your data to give you the output you want. Please see [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example. – Ben Nov 18 '21 at 01:33

1 Answers1

0

On top of the given answers for dplyr here is one using data.table which is handy if speed and/or large amount of data is of concern.

require(data.table)     # load package
    
setDT(df)               # set dataframe as data table

new_df <-
  df[, .(stat = mean(stat1)
         , games_played = .N
         ), by = home_team_name]
Sweepy Dodo
  • 1,761
  • 9
  • 15