I have the following sample dataframe of baseball game attendance:
team <- rep("SFG", 6)
attendance <- seq(1000:1005)
year <- c(2000, 2000, 2001, 2001, 2002, 2002)
dates <- as.Date(c("2000-01-01", "2000-01-02", "2001-01-03", "2001-01-04", "2002-02-05", "2002-03-06"))
df <- data.frame(team, attendance, year, dates)
I would like to create two new columns.
The first would be a simple binary variable called "opening_day" that indicates (with 1 or 0) whether or not the game is the first game of the year.
The second would be called "opening_day_attendance" and would contain the attendance number of that first game. In other words, all rows for 2000 would have the attendance for that first game of the year.
If possible I'd like to achieve this with dplyr and piping.
Thanks so much!
EDIT: Others have noted that this is a possible duplicate question, and while it is generally similar to the one mentioned, most of the answers to the other question A) Don't create a new column in the original dataset with the values and B) Don't use dplyr and piping to achieve the results.