Consider the following data:
library(tidyverse)
GameID <- c('Bos001', 'Bos002', 'Bos003', 'Pit001', 'Pit002', 'Pit003')
Stadium <- c("Fenway Park", NA, "Fenway Park", NA, NA, "PNC Park")
GameData <- data.frame(GameID, Stadium)
GameData
GameID Stadium
1 Bos001 Fenway Park
2 Bos002 <NA>
3 Bos003 Fenway Park
4 Pit001 <NA>
5 Pit002 <NA>
6 Pit003 PNC Park
The Stadium column has a relationship with the GameID column. In this contrived example:
- All rows where GameID begins with "Bos", the Stadium column value should be "Fenway Park".
- All rows where GameID begins with "Pit", the Stadium column value should be "PNC Park".
Tidied Data:
GameID Stadium
1 Bos001 Fenway Park
2 Bos002 Fenway Park
3 Bos003 Fenway Park
4 Pit001 PNC Park
5 Pit002 PNC Park
6 Pit003 PNC Park
How can I fill in these values?
Should I use a combination of dplyr:arrange()
and tidyr:fill()
?