-1

I have created a while loop that is being executed across a sizable data set. The loop is as such:

i = 1
while(i<=m){
  Date = London.Events$start_time[i] 
  j=1
  while(j<=n){
    Hotel = London.Hotels$AS400.ID[j]
    Day.Zero[i,j] = sum(London.Bookings$No.of.Rooms[London.Bookings$Stay.Date == Date & London.Bookings$Legacy.Hotel.Code == Hotel])
  j=j+1
  }
i=i+1
}

Where:

m = 9957 #Number of Events
n = 814 #Number of Hotels

Day.Zero = as.data.frame(matrix(0, 9957, 814))

Briefly explained, for each combination of date and hotel (pulled from two other data frames), produce the sum from the column London.Bookings$No.of.Rooms and deposit that into the corresponding row of the matrix.

The loop appears to run without error, however when stopping it after 5 mins+ it is still running and nowhere near complete!

I would like to know how one of the apply family of functions could be used as a replacement here for much faster completion.

Thanks!

Qaribbean
  • 178
  • 2
  • 3
  • 17
  • 5
    Example datasets would be really helpful here. Please see [how to write a great R reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – C8H10N4O2 Feb 29 '16 at 16:02

2 Answers2

1

Probably,

xtabs(No.of.Rooms ~ Stay.Date + Legacy.Hotel, data = London.Bookings)

gets you something similar to what you want.

Kota Mori
  • 6,510
  • 1
  • 21
  • 25
0

Using library dplyr, you can do something like the following (assuming your input data frame has such column names - vaguely interpreted from your code / question):

library(dplyr)
London.Bookings %>% group_by(Legacy.Hotel.Code, Stay.Date) %>% summarise(Total.No.of.Rooms = sum(No.of.Rooms))
Gopala
  • 10,363
  • 7
  • 45
  • 77