0

I have a data set that has individual basketball players statistics for each team for 17 years. In R I am trying to turn these player level observations into team level observations (for each year) by using a for loop which iterates through year and team and then aggregates the top three scorer's individual statistics (points, assists, rebounds etc). How would you recommend I proceed? (below you will find my current attempt, it only pulls the observations from the last teams and year of the data set and can't pull other statistics such as assists and rebound numbers from the 3 top scorers).

for (year in 2000:2017) {

  for (team in teams) {

ts3_points =top_n(select(filter(bball, Tm == team & Year == year), PPG),3)


  }
}
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 2
    Welcome to SO! Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Oct 24 '19 at 23:12

1 Answers1

0

Would be more helpful with your data but I don't think you will need to have two for loops. Just need to use dplyr. Below I used some dumby data to try to recreate your issue...

colname key:

month == years
carrier == team
origin == player
library(dplyr)
library(nycflights13) # library with dumby data

flights %>% 
  group_by(month, carrier, origin) %>% 
  summarise(hour_avg = mean(hour)) %>%  # create your summary stats
  arrange(desc(hour_avg)) %>% #sort or data by a summary stat
  top_n(n = 3) # return highest  hour_avg

# returns the highest hour_avg origin (player) for every month and carrier (year and team)

Hope this helps!

David
  • 2,200
  • 1
  • 12
  • 22