Vincent Levinger Vincent Levinger - 1 month ago 8
R Question

Average columns in reverse, and return sum of those averages in R

I'm just starting to learn R for forecasting and analysis purposes, and I've decided to try and create a full package for the forecasting model I'm using (Additive Pickup). I work for a hotel, and one of the things I do on a regular basis is forecast our demand, so this will certainly make this part of my job faster and easier!

I've already created a few functions that will get me a data frame of my pickup numbers, and now I'm working on a function to average a user defined number of columns in that new data frame. I've included code to create some sample data, and the code I'm working on below.

Sample Data:

test = data.frame(replicate(10, sample(0:2, 32, rep = TRUE)))


Broken Code:

averagePickup = function(data, day, periods) {
# data will be your Pickup Data
# day is the day you're forecasting for (think row number)
# periods is the period or range of periods that you need to average (a column or range of columns).
pStart = ncol(data)
pEnd = ncol(data) - periods
row = (day-1)
new_frame = as.data.frame(matrix(nrow = 1, ncol = periods))

for(i in pStart:pEnd) {
new_frame[1,i] = mean(data[1:row , i])
}
return(sum(new_frame[1,1:i]))
}


The goal of this is to iterate backwards from the last column in the data to a user defined period. For example, setting "periods" to 1 should return the sum of the average of the last column only. Setting it to 2 would yield the sum of the averages of the last column and second to last column.

However, when I try to run a test of this I get an error that reads


Error in [<-.data.frame
(
tmp`, 1, i, value = 0.9) : new columns
would leave holes after existing columns


Any advice you guys could lend would be so appreciated. Also, let me know if I made absolutely zero sense, and apologies for the essay on this question... Note that this has to iterate backwards because of the way the input data is formatted.

Answer

I think this is what you want:

averagePickup = function(data, day, periods) {
  # data will be your Pickup Data
  # day is the day you're forecasting for (think row number)
  # periods is the period or range of periods that you need to average (a column or range of columns).
  pStart = ncol(data)
  pEnd = ncol(data) - (periods-1)
  row = (day-1)
  new_frame <- as.data.frame(matrix(nrow = 1, ncol = periods))

  for(i in pStart:pEnd) {
    new_frame[1,1+abs(ncol(data)-i)] <- mean(data[1:row , i])
  }
  return(sum(new_frame[1,1:ncol(new_frame)]))
}

averagePickup(test,1,5)

[1] 7

Comments