Reputation: 1
This is the prompt I am working from:
In the state population data, can you write a loop that pulls only states with populations between 200,000 and 250,000 in 1800? You could accomplish this a few different ways. Try to use next and break statements.
The dataset in question tracks how each state's population has shifted from year to year, decade to decade.
There are various columns representing each year of observations, including one for 1800 ("X1800"). The states are all also listed under one column ("state," of course).
I've been working on this one for quite a while, being new to coding. Right now, the code I have is as follows:
i <- 1
for (i in 1:length(statepopulations$X1800)) {
if ((statepopulations$X1800[i] < 200000) == TRUE)
next
if ((statepopulations$X1800[i] > 250000) == TRUE)
break
print (statepopulations$state[i])
}
I want to print the names of all states that fall within that population range for the year 1800.
I'm not sure where I'm going wrong. Any help is appreciated.
I keep getting a message that I'm "missing value where TRUE/FALSE needed."
Upvotes: 0
Views: 45
Reputation: 9
in the condition, you don't need the == TRUE part.
(statepopulations$X1800[i] < 200000) should work by itself -- it will return a TRUE or FALSE, which will dictate what happens next
Upvotes: 0