I am trying to create a data frame with predefined columns and to be able to populate the columns accordingly.
I have the following code that creates a data frame with 3 identical columns, initially populated with NAs and then further filled according to the loops (again the same loops but refering to different columns) :
#Parameters
Forecast.Days = 200
MBL = 500
#Construct share of room nights by group component table
Share.of.Room.Nights = data.frame(Destination.1 = c(rep(NA, times = Forecast.Days)), Destination.2 = c(rep(NA, times = Forecast.Days)),
Destination.3 = c(rep(NA, times = Forecast.Days)))
#Destination 1
for (i in 1:length(Share.of.Room.Nights$Destination.1)){
if (Future.Confirmed.Bookings$Total[i] >= MBL){
Share.of.Room.Nights$Destination.1[i] = Future.Confirmed.Bookings[ ,3][i]/Future.Confirmed.Bookings$Total[i]
} else {
Share.of.Room.Nights$Destination.1[i] = Confirmed.Bookings[, 2][i]/Confirmed.Bookings$Total[i]
}
}
#Destination 2
for (i in 1:length(Share.of.Room.Nights$Destination.2)){
if (Future.Confirmed.Bookings$Total[i] >= MBL){
Share.of.Room.Nights$Destination.2[i] = Future.Confirmed.Bookings[ ,4][i]/Future.Confirmed.Bookings$Total[i]
} else {
Share.of.Room.Nights$Destination.2[i] = Confirmed.Bookings[ ,3][i]/Confirmed.Bookings$Total[i]
}
}
#Destination 3
for (i in 1:length(Share.of.Room.Nights$Destination.3)){
if (Future.Confirmed.Bookings$Total[i] >= MBL){
Share.of.Room.Nights$Destination.3[i] = Future.Confirmed.Bookings[ ,5][i]/Future.Confirmed.Bookings$Total[i]
} else {
Share.of.Room.Nights$Destination.3[i] = Confirmed.Bookings[ ,4][i]/Confirmed.Bookings$Total[i]
}
}
I would like to be able to set a parameter for the number of those Destination columns to create in the initial data frame, in this case 3 (max would be 6), and have the code that then only runs the required number of loops (the code would be there for 6 columns but in this case would only run 3.
Is this possible?
Thanks