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
my_df <- data.frame(matrix(nrow=3,ncol=10))
my_df
# X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
# 1 NA NA NA NA NA NA NA NA NA NA
# 2 NA NA NA NA NA NA NA NA NA NA
# 3 NA NA NA NA NA NA NA NA NA NA
class(my_df)
# [1] "data.frame"
dim(my_df)
# [1] 3 10
# If column names are available
names(my_df) <- LETTERS[1:10]
my_df
# A B C D E F G H I J
# 1 NA NA NA NA NA NA NA NA NA NA
# 2 NA NA NA NA NA NA NA NA NA NA
# 3 NA NA NA NA NA NA NA NA NA NA
my_df<- data.frame(x= character(0), y= numeric(0), a = character(0), b= integer(0))
str(my_df)
# 'data.frame': 0 obs. of 4 variables:
# $ x: Factor w/ 0 levels:
# $ y: num
# $ a: Factor w/ 0 levels:
# $ b: int