I am hoping someone can help me come up with a solution to my problem. I have a data frame that I'm looking to add 2 new sets of values based on the existing data frame.
- Weighted Average to come up with a composite weighted average by stat (2014 = .4, 2015 = .4, and 2016 = .2) multiplied by the grouped stat (HR, R, RBI, SB)
- 3 Year Avg (Same Idea as above, but just a straight 3 year mean of the 3 most current year)
I'd like the new data to be identified under the season column.
Here's the data.frame
:
full_table_raw <- structure(list(playerID = c("abreujo02", "abreujo02",
"abreujo02", "abreujo02", "abreujo02", "abreujo02", "abreujo02",
"abreujo02", "abreujo02", "abreujo02", "abreujo02", "abreujo02",
"arenano01", "arenano01", "arenano01", "arenano01", "arenano01",
"arenano01", "arenano01", "arenano01", "arenano01", "arenano01",
"arenano01", "arenano01", "blackch02", "blackch02", "blackch02",
"blackch02", "blackch02", "blackch02", "blackch02", "blackch02",
"blackch02", "blackch02", "blackch02", "blackch02"), season = c(2014L,
2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L,
2016L, 2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L,
2016L, 2016L, 2016L, 2016L, 2014L, 2014L, 2014L, 2014L, 2015L,
2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 2016L), stat = c("HR",
"R", "RBI", "SB", "HR", "R", "RBI", "SB", "HR", "R", "RBI", "SB",
"HR", "R", "RBI", "SB", "HR", "R", "RBI", "SB", "HR", "R", "RBI",
"SB", "HR", "R", "RBI", "SB", "HR", "R", "RBI", "SB", "HR", "R",
"RBI", "SB"), points = c(3, 2, 3, 2, 2, 1, 2, 1, 1, 1, 2, 1,
1, 1, 1, 1, 3, 3, 3, 2, 3, 3, 3, 2, 2, 3, 2, 3, 1, 2, 1, 3, 2,
2, 1, 3), ranks = c(1, 2, 1, 2, 2, 3, 2, 3, 3, 3, 2, 3, 3, 3,
3, 3, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 3, 2, 3, 1, 2, 2, 3,
1), value = c(36, 80, 107, 3, 30, 88, 101, 0, 25, 67, 100, 0,
18, 58, 61, 2, 42, 97, 130, 2, 41, 116, 133, 2, 19, 82, 72, 28,
17, 93, 58, 43, 29, 111, 82, 17)), class = "data.frame", row.names = c(NA,
-36L))