-1

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

Qaribbean
  • 178
  • 2
  • 3
  • 17
  • Zheyuan Li - Thank you for this, I can see how that creates the pre-defined columns I need - but how would I automatically label the columns so as to be able to run the following loops? I still also need to be able to populate the columns with the required number of loops. – Qaribbean Jun 23 '16 at 10:01

2 Answers2

1
    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 
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
0

Using the previously suggested answer of

as.data.frame(matrix(0, nrow = ?, ncol = ?))

the parameter can be entered to create the predefined data frame

Components = 3
Forecast.Days = 200    

Share.of.Room.Nights = as.data.frame(matrix(0, nrow = Forecast.Days, ncol = Components)) 

Using if("Destination.1" %in% colnames(Share.of.Room.Nights)) allows determination of whether the column exists, after which the loops can be run to populate the columns that exist depending on the pre-defined parameters.

Qaribbean
  • 178
  • 2
  • 3
  • 17