flying_pumpkin
flying_pumpkin

Reputation: 13

R - How do you get a variable to iterate through i in a for loop?

I'm fairly new to R and trying to use a for loop to model estimated profit for an airline based on different levels of overbooking. I want my for loop to assign each value of i from 1:50 for # of tickets sold, which is used in other variables later in the loop.

Fare<-200
Seats<-132
Refund<-0.5
BumpCost<-2.5
NSRate<-.1
for(i in 1:50){
  Tickets<-(Seats+i)
  NoShows<-rbinom(50,Tickets,NSRate)
  Arrivals<-Tickets-NoShows
  Bumps<-max(0,Tickets-NoShows-Seats)
  Profit<-Tickets*Fare-NoShows*Fare*Refund-Bumps*Fare*BumpCost
  Profits<-as.data.frame(cbind(Tickets,NoShows,Arrivals,Bumps,Profit))
}

My problem is that Tickets is showing 182 in every row, when it should start at 133 and go through 182. How do I fix my Tickets variable to properly iterate through each value of i?

   Tickets NoShows Arrivals Bumps Profit
1      182      16      166    39  15300
2      182      21      161    39  14800
3      182      17      165    39  15200
4      182      23      159    39  14600
5      182      25      157    39  14400
6      182      12      170    39  15700
7      182      18      164    39  15100
8      182      23      159    39  14600
9      182      19      163    39  15000
10     182      18      164    39  15100

Also, at a high level, how do for loops work? With my code, is it running Tickets for i 1:50 before moving on to NoShows/Arrivals/etc, or does it run Tickets for i=1 and then move down the block before doing Tickets for i=2?

Upvotes: 0

Views: 65

Answers (1)

Jon Spring
Jon Spring

Reputation: 66445

As you've set it up, Tickets is a single element vector which you are overwriting with each loop, so it ends with the final value.

It doesn't look like you need a loop here at all. The more idiomatic way to do this in R would be to define vectors for each of the variables like below:

Fare<-200
Seats<-132
Refund<-0.5
BumpCost<-2.5
NSRate<-.1

Seat <- 1:50
Tickets<- (Seats + Seat)
NoShows<-rbinom(50,Tickets,NSRate)
Arrivals<-Tickets-NoShows
Bumps<-pmax(0,Tickets-NoShows-Seats)  # max would be max across all rows, vs pmax will find the max across the current vector element
Profit<-Tickets*Fare-NoShows*Fare*Refund-Bumps*Fare*BumpCost
Profits<-as.data.frame(cbind(Tickets,NoShows,Arrivals,Bumps,Profit))

> head(Profits)
  Tickets NoShows Arrivals Bumps Profit
1     133      16      117     0  25000
2     134      12      122     0  25600
3     135      16      119     0  25400
4     136      15      121     0  25700
5     137      14      123     0  26000
6     138      11      127     0  26500

In some unusual cases it makes more sense to use a loop, in which case you could do it like this. The important part is that we create 50-element vectors to fill with each loop cycle.

Tickets = NoShows = Arrivals = Bumps = Profit = rep(NA, 50)
for(i in 1:50){
  Tickets[i]<-(Seats+i)
  NoShows[i]<-rbinom(50,Tickets[i],NSRate)
  Arrivals[i]<-Tickets[i]-NoShows[i]
  Bumps[i]<-max(0,Tickets[i]-NoShows[i]-Seats)
  Profit[i]<-Tickets[i]*Fare-NoShows[i]*Fare*Refund-Bumps[i]*Fare*BumpCost
  Profits<-as.data.frame(cbind(Tickets,NoShows,Arrivals,Bumps,Profit))
}

Upvotes: 2

Related Questions