-2

I have a data set that contains NBA injury data from 2010 to 2018. The data was presented "by player", and I'm trying to aggregate it. so that it's "by team". I also have injuries by player, and want to convert the injuries to "by team" as well. How would I be able to create this dataset? Thank you for your help!

dput(injuries[1:20,c(2,6)])
structure(list(Team = c("Chicago Bulls", "Detroit Pistons", "Detroit Pistons", 
"Portland Trail Blazers", "New Jersey Nets", "Detroit Pistons", 
"Detroit Pistons", "Denver Nuggets", "Milwaukee Bucks", "Sacramento Kings", 
"Charlotte Bobcats", "Golden State Warriors", "Phoneix Suns", 
"Miami Heat", "Oklahoma City Thunder", "Minnesota Timberwolves", 
"Miami Heat", "Milwaukee Bucks", "Milwaukee Bucks", "Memphis Grizzlies"
), body_part = c("finger", "achilles", "toe", "knee", "back", 
"achilles", "toe", "foot", "hip", "groin", "leg", "finger", "finger", 
"hand", "knee", "back", "ankle", "ankle", "ankle", "back")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

oig
  • 1
  • 1
  • Hi oiguwop, welcome to StackOverflow! Could you share a part of your data frame using `dput()`? See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to make a great reproducible example – Bas Apr 29 '20 at 06:25
  • You don't "subset" a data frame from individuals to groups - you _aggregate_ it. That means you need to determine what information to show for each team, for example, number of injured players, most common injury, etc... – Edward Apr 29 '20 at 06:30
  • Hey @Bas, I edited the post to include the two columns I was referring to – oig Apr 29 '20 at 06:49
  • The next step, as Edward said, is to determine what information you want to show for each team. It is important you make your question as specific as possible: an 'expected output' would help. – Bas Apr 29 '20 at 06:56
  • Sorry, first post here, I want the data to be arranged so that the variables would be "Team" and then each type of injury would identified as counts. For example, it would be able to identify that the Chicago Bulls had 144 ankles injuries, 300 upper body injuries, and so on – oig Apr 29 '20 at 07:15

2 Answers2

0

You could use table to count the injury count by Team :

table(injuries$Team, injuries$body_part)

Using dplyr this can be done as :

library(dplyr)
injuries %>% count(Team, body_part)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can use summarise with group_by

library(dplyr)
injuries %>%
   group_by(Team, body_part) %>%
   summarise(n = n())
akrun
  • 874,273
  • 37
  • 540
  • 662