I am working with a large panel data set resembling the following:
I am looking to reshape the data and add columns that reflect values of the Score
and Distance
columns for every fifth value of Game (grouped by Player
and Year
). So the final data set would look like:
Normally, I would do this using the match command:
data %>%
group_by(Player, Year) %>%
mutate(ScoreGame0 = Score[match(Game, 0)]) %>%
mutate(ScoreGame5 = Score[match(Game, 5)])
And so on. However, given that my final data set is much larger than this (involving way more observations and variables to reshape), I was wondering if there was an easier way to do this. Perhaps it is possible to write a function? (I have tried and failed miserably at that).
Initial data:
structure(list(Player = c("Lebron James", "Lebron James", "Lebron James",
"Lebron James", "Lebron James", "Lebron James", "Lebron James",
"Lebron James", "Lebron James", "Lebron James", "Lebron James",
"Lebron James", "Steph Curry", "Steph Curry", "Steph Curry",
"Steph Curry", "Steph Curry", "Steph Curry", "Steph Curry", "Steph Curry",
"Steph Curry", "Steph Curry", "Steph Curry", "Steph Curry"),
Year = c(2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L),
Game = c(0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L,
0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L), Score = c(32L,
23L, 33L, 27L, 29L, 27L, 44L, 22L, 33L, 54L, 65L, 12L, 45L,
48L, 44L, 51L, 65L, 76L, 76L, 67L, 56L, 44L, 21L, 32L), Distance = c(12L,
21L, 20L, 33L, 17L, 13L, 79L, 18L, 19L, 63L, 45L, 34L, 18L,
23L, 22L, 77L, 34L, 42L, 88L, 65L, 37L, 92L, 35L, 54L)), class = "data.frame", row.names = c(NA,
-24L))