Adding rows with changing variable values in R
I have the following extract of my dataset about the occupancy of a football match:
example <- data.frame(Date <- c("2019-03-21", "2019-03-30", "2019-04-07", "2019-03-21", "2019-03-30", "2019-04-07", "2019-03-21", "2019-04-07", "2019-03-21", "2019-03-30", "2019-04-07", "2019-03-21", "2019-03-30", "2019-04-07", "2019-03-21", "2019-03-30", "2019-04-07", "2019-03-21", "2019-03-30", "2019-04-07", "2019-03-21", "2019-03-30", "2019-04-07", "2019-03-21", "2019-03-30", "2019-03-21", "2019-03-30", "2019-03-21", "2019-03-30", "2019-03-21"), Block <- c("43L","43L", "43L", "15B", "15B", "15B", "43L", "43L", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B", "15B"), Preis <- c("24", "35", "30", "35", "45", "40", "26", "30", "35", "45", "40", "34", "43", "42", "35", "42", "45", "36", "45", "43", "36", "43", "40", "35", "41", "32", "42", "30", "42", "35"), Max <- c("3", "3", "3", "10", "10","10","3", "3", "10", "10","10", "10", "10","10", "10", "10","10", "10", "10","10", "10", "10","10", "10", "10", "10", "10", "10", "10", "10"), Actual <- c("2", "1", "2", "10", "9", "6","2", "2", "10", "9", "6", "10", "9", "6", "10", "9", "6", "10", "9", "6", "10", "9", "6", "10", "9", "10", "9", "10", "9", "10"), Temperatur <- c("15", "20", "18","15", "20", "18", "15", "18", "15", "20", "18", "15", "20", "18", "15", "20", "18", "15", "20", "18", "15", "20", "18", "15", "20", "15", "20", "15", "20", "15"), <PERSON> <- c("1", "1", "1", "1", "1","1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1") ) colnames(example) <- c("Date", "Block", "Price", "Max", "Actual", "Temperature", "Placesold")
In reality, the dataset contains over 100 blocks and 46 different dates. If you take a closer look at the data, you can see that different numbers of seats are sold out in block 15B and 43L on different days. 60
``` table(example$Date, example$Block) table(example$Placesold)
15B 43L
2019-03-21 10 2 2019-03-30 9 1 2019-4-07 6 2
table(example$Placesold)
1 30 ```
My goal is to add the seats that were not sold to the data set. The variable Placesold should be 0 instead of 1. In addition, the average price of the sold tickets should be used instead of the price (without 0). 946