0

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)
isomorphismes
  • 8,233
  • 9
  • 59
  • 70
  • Without more information about how are you reading the data this exercise is useless. Please, provide a ready-to-go MWE where you read a sample of the data and also provide that data as a file or as a in-line executed data frame. – epsilone Oct 13 '14 at 15:02
  • That data is the data I printed out after I read in the original csv file. I don't think there is any issue involved in the importing process. – miaoxingren Oct 13 '14 at 17:21

1 Answers1

1

The potential reasons are two:

1) There is no $time.population level on the state list. This creates a NULL variable which is processed by min, returning that warning message. Try it for yourself:

min(NULL)

2) (Most likely) The variable look.at is a numeric(0) because the logical equality state$time.zone.1 == zone is never satisfied, so it returns that value. Check it for yourself:

min(numeric(0))

To prevent both cases, avoid computing the min of such vectors adding a conditional, so you to compute the minimum only if !is.null(look.at) (first point) and length(look.at)!=0 (second point) are satisfied.


EDIT: There are other several things that may cause the problem:

1) state$population <- as.numeric(gsub("\\,","",state$population)) this could potentially return a numeric(0).

2) Another strange thing, you do a conversion to characterhere:

the.zones <- unique(state$time.zone.1)
the.zones <- as.character(the.zones) 

But then you compare the original data (state$time.zone.1) with the converted to character (zone in the.zone), which definitely is not the safest way to do comparisons and may lead to mismatchs if a bad conversion happens:

for(zone in the.zone){
   look.at <- state$time.zone.1 == zone
   ...
}

Either convert state$time.zone.1 to character or do not convert the.zones.

epsilone
  • 745
  • 11
  • 23
  • Thanks for the responses! I have full data for population (no missing data, and it's continuous numeric). I mentioned in my question earlier that I have run the program step by step myself and it worked out; which means I ran the code like below and state$time.zone.1 == zone were all satisfied: look.at <- state$time.zone.1 == "CST (UTC-6)"; low <- append(low,min(state$population[look.at])); high <-append(high,max(state$time.population[look.at])). Each time I changed the value of the zone to the actual level and the equations were all satisfied. – miaoxingren Oct 12 '14 at 13:30
  • I edited the answer with other things that will good to look at. If it does not work you will need to provide a MWE (i.e., a small part of the data) where you get the error. Checking it manually does not imply that is not going to fail for a particular zone. Good luck! – epsilone Oct 12 '14 at 16:41
  • I just added the data to the bottom of my question. It is good point that the.zones and the time.zone.1 might not have the same type - I just tried to change the time.zone.1 to character first and then create the the.zones from the character time.zone.1 but it still didn't work. I've added the new program as 'New lines' in my question window. The "state$population <- as.numeric(gsub("\\,","",state$population))" was just to remove the comma in the data and it worked out well. – miaoxingren Oct 13 '14 at 12:20