0

I'm trying to get a loop to pull only states with populations between 200,000 and 250,000. How can I do this with next and break statements? I feel like I have the right idea (population > 200,000) and (population < 250,000), but it's not working out for me.

The dataset tracks how much state populations have shifted from one year to the next, and has columns with each year (like for 1800, the year I'm looking at).

There are also columns for each state.

I'm not sure what I expected - a few states? tons of states? But instead I keep getting errors about my else if statement - saying that else was found in an area where it wasn't expected to be.

Jonas
  • 121,568
  • 97
  • 310
  • 388
riggsy
  • 9
  • 2
  • Hi riggsy, please read through [How to make a great R reproducible example](https://stackoverflow.com/q/5963269/17303805) to learn more about providing a good [mre] for R questions. It’s usually hard to help much unless you share your data and code. – zephryl Feb 08 '23 at 04:26
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 08 '23 at 21:14

1 Answers1

0

Given by your description, it seems that you only need states that have population between 200,000 and 250,000 regardless of the year.

Your dataset is like this

-------------------------
|state| population| year|
-------------------------
|NY   | 220,000   | 2018|
-------------------------
.........
.........
.........

So you can do something like this (assuming your dataset is a list of list (2D matrix))

for i in dataset:
    if 200000 < i[1] < 250000:
        print('State: ', i[0])
        print('Population: ', i[1])
        

This will only print rows that satisfies the population condition. Later, instead of print you can put your own logic.

khushmeeet
  • 56
  • 3