-2

I currently have a table for NFL player stats. Column A is Player Position, Column B is Player Name, Column C is player projected rank for that week, Column D is how many fantasy points they got that week. Something like this..

pos <- c("QB", "QB", "QB", "RB", "RB")
name <- c("Tom Brady", "Matt Ryan", "Aaron Rodgers", "Leveon Bell", "Devonta Freeman")
proj <- c(20.5, 18.5, 21.3, 22.5, 16.5)
actual <-c (15, 21.4, 19.0, 15.1, 12.4)
rawdata <- data.frame(pos, name, proj, actual)

Since there are 8 weeks so far, each player has 8 rows of data (1 per week). How can I consolidate this so that I can get the average projection and actual data for any given individual? Ex: I want to get the mean projection (Column C) and mean actual data (Column D) for Matt Ryan (Column B)

Bonus: What if I wanted to get the mean projection (Column C) and mean actual data (Column D) for any given position? (Column A)

Thanks!

Jaap
  • 71,900
  • 30
  • 164
  • 175
Kyle L
  • 19
  • 4

1 Answers1

-1

Using dplyr/tidyverse you would approach the first task as follows:

rawdata %>%
group_by(name) %>%
summarize(meanProjection = mean(proj),
          meanActual = mean(actual)) -> summarizedDataByName

The second task obtains as

rawdata %>%
group_by(pos) %>%
summarize(meanProjection = mean(proj),
          meanActual = mean(actual)) -> summarizedDataByPosition
CMichael
  • 1,684
  • 14
  • 18
  • Great. Let's say I do the first one to group all the data by Players. Your code gives me a new data frame. From there, how can I go about re-importing every player's position (found in the old dataframe "rawdata") into the new data frame "summarizedDataByName"? Thanks! – Kyle L Nov 04 '17 at 15:30