# R Control Flow Help

I’m going through some R exercises right now and I’m really confused on what I’m doing wrong here. So the goal is to use control flow to find the average number of passengers from all flights that have more scheduled departures than performed departures.

When I ran this code the answer that I got was 3100636. From what I understand the answer should be somewhere around 66.

The data that I’m working off of is: “US Airport Statistics,” submitted by Larry Winner, University of Florida.

``````counter = 0
total_passengers = 0
for (i in 1:135)
{

if (airport\$Scheduled.Departures[i] < airport\$Performed.Departures[i])
{
counter = counter + 1
total_passengers = total_passengers + airport\$Passengers[i]
}
avg_passengers = total_passengers/counter
}
print(avg_passengers)
``````

From what I understand this code should find the airports that have less scheduled departures than performed departures. Then it takes the row value of the row where there are less scheduled departures than performed departures, find those passengers, adds them up and divides them by the counter which should only increment when the if statement triggers.

I’m not sure what I’m doing wrong here. I feel that this would work in python.

Any help/guidance would be appreciated.

So I’ve, looked back at my code and I think that I’m now using the right number for the denominator. But I’m still not sure what the best solution for the for loop is. The answer should be 66, but I’m still not getting it.
I’ve tried:

``````for (i in 1:135)     - This got me the closest answer with 67.35995

for (i in nrow(airport))  - This got me 40 average passengers

for (i in dim(airport[1])  - This got me the answer of 77 average passengers.
``````

I’m confused as to why I’m getting different answers for these 3 different pieces of code, as its my understanding that they should be returning the same number for the loop to loop through.

I guess I’ll go through it one more time with print statements and inspect the output. But any advice would be appreciated.

Hi @charlesd can you please provide the dataset, or a link to the dataset? Thanks!

Sure thing, I should have thought of that beforehand.

The dataset is 135 rows long, I’ll post the first 15 rows and then a link to the dataset.

If you’d like the full dataset: airport.csv (12.3 KB)

By the way, I’ve changed the code. I had the counter all wrong. I still have an issue with which method I should use to find the length of the dataset.

``````counter = 0
total_passengers = 0
Tot_Scheduled_Departures = 0
#i in 1:135    I got 67.35 for this. This is the closest.
#I get a different number if I use nrow(airport) why is that? I got 40 for this.
# I got 77 fir dun(airport[1])
for (i in 1:135)
{

if (airport\$Scheduled.Departures[i] < airport\$Performed.Departures[i])
{
Tot_Scheduled_Departures = Tot_Scheduled_Departures + airport\$Scheduled.Departures[i]
total_passengers = total_passengers + airport\$Passengers[i]
}
avg_passengers = total_passengers/Tot_Scheduled_Departures
}
print(avg_passengers)
``````

I’ve started to use print statements to troubleshoot and pinpoint the problem. But it’s a bit clunky. What’s the best way to troubleshoot R code?

Thanks.

Thanks @charlesd! I will try to take a look at this on Tuesday. In the meantime, as a way to troubleshoot R code, I recommend setting a value for `i` and then going through each step in your `if` statement to see if it is working as desired.

For example, you could start with `i <- 1` to see if the values from the 1st row of the dataset are pulled in as expected, and then go through line-by-line to check the values returned at each step. Then set `i <- 2` and see if the values change and properly reflect the collective average from row 1 & 2 combined. In other words, avoid running the for-loop, but run each line of your code step-by-step.

This other post from the Community may also be helpful for learning about how to test single iterations in a for loop.

1 Like

Don’t bother. It was my mistake.

First, I was using nrow(airport) and not `1:nrow(airport).`

The second thing that I was doing wrong was using scheduled departures as a the denominator and not performed departures.

So it should look something like this:

``````counter = 0
total_passengers = 0
Tot_Performed_Departures = 0
for (i in 1:nrow(airport))
{

if (airport\$Scheduled.Departures[i] < airport\$Performed.Departures[i])
{
Tot_Performed_Departures = Tot_Performed_Departures + airport\$Performed.Departures[i]
total_passengers = total_passengers + airport\$Passengers[i]
}
avg_passengers = total_passengers/Tot_Performed_Departures
}
print(avg_passengers)
``````

Hope this helps someone in the future. Thanks for offering your help, casey.

Nicely done @charlesd!