I have a simple data for practice and when I tried to calculate the max, min population of each time zone as below, I got warning messages like "In max(state$time.population[look.at]) :no non-missing arguments to max; returning -Inf". I tried to run the loop one by one by manually changing the "zone" each time and they all worked. I am not sure what is the reason then. There are spaces for each level of zone so I wonder if that is the cause - I tried to change it to character, but it still didn't work...anyone knows how I could fix this?
state <- read.csv("states.csv")
state$population <- as.numeric(gsub("\\,","",state$population))
/* the.zones <- unique(state$time.zone.1) the.zones <- as.character(the.zones)*/
/New lines/
state$time.zone.1 <- as.character(state$time.zone.1)
the.zones <- unique(state$time.zone.1)
low <- c()
high <-c()
for (zone in the.zones){
look.at <- state$time.zone.1 == zone
low <- append(low,min(state$population[look.at]))
high <-append(high,max(state$time.population[look.at]))
}
low
high
Result:
Warning messages:
1: In max(state$time.population[look.at]) :
no non-missing arguments to max; returning -Inf
2: In max(state$time.population[look.at]) :
no non-missing arguments to max; returning -Inf
3: In max(state$time.population[look.at]) :
no non-missing arguments to max; returning -Inf
4: In max(state$time.population[look.at]) :
no non-missing arguments to max; returning -Inf
5: In max(state$time.population[look.at]) :
no non-missing arguments to max; returning -Inf
6: In max(state$time.population[look.at]) :
no non-missing arguments to max; returning -Inf
Other info: Levels of time zones: Levels: AKST (UTC-09) CST (UTC-6) EST (UTC-5) HST (UTC-10) MT (UTC-07) PT (UTC-8) If change to characters: "CST (UTC-6)" "AKST (UTC-09) " "MT (UTC-07)" "PT (UTC-8)" "EST (UTC-5)" "HST (UTC-10) "
What the data looks like:
name abbreviation capital most.populous.city population square.miles time.zone.1
1 ALABAMA AL Montgomery Birmingham 4,708,708 52,423 CST (UTC-6)
2 ALASKA AK Juneau Anchorage 698,473 656,425 AKST (UTC-09)
3 ARIZONA AZ Phoenix Phoenix 6,595,778 114,006 MT (UTC-07)
4 ARKANSAS AR Little Rock Little Rock 2,889,450 53,182 CST (UTC-6)
5 CALIFORNIA CA Sacramento Los Angeles 36,961,664 163,707 PT (UTC-8)
6 COLORADO CO Denver Denver 5,024,748 104,100 MT (UTC-07)