for循环因子变量R(级别是带空格的字符)返回警告消息“没有非缺失的参数到max”

时间:2022-12-05 19:19:45

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?

我有一个简单的练习数据,当我尝试计算每个时区的最大,最小人口如下时,我收到了警告信息,例如“In max(state $ time.population [look.at]):no non-missing max的参数;返回-Inf“。我试图通过每次手动更改“区域”逐个运行循环,它们都工作。我不确定那是什么原因。每个级别的区域都有空间,所以我想知道这是否是原因 - 我试图将其改为角色,但它仍然无效......任何人都知道如何解决这个问题?

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)*/

/ * 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) "

其他信息:时区级别:级别:AKST(UTC-09)CST(UTC-6)EST(UTC-5)HST(UTC-10)MT(UTC-07)PT(UTC-8)如果更改为字符:“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)

1 个解决方案

#1


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:

1)州名单上没有$ time.population等级。这将创建一个NULL变量,由min处理,返回该警告消息。亲自尝试一下:

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:

2)(最有可能)变量look.at是一个数字(0),因为永远不会满足逻辑相等状态$ time.zone.1 == zone,所以它返回该值。自己检查一下:

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.

为了防止这两种情况,避免计算添加条件的这种向量的min,所以只有在!is.null(look.at)(第一点)和长度(look.at)!= 0(第二点)时才计算最小值)很满意。


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).

1)state $ population < - as.numeric(gsub(“\\,”,“”,state $ population))这可能会返回一个数字(0)。

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

2)另一个奇怪的事情,你转换为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:

但是,然后将原始数据(state $ time.zone.1)与转换为字符(.zone中的区域)进行比较,这绝对不是最安全的比较方式,如果发生错误转换,可能会导致不匹配:

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.

将state $ time.zone.1转换为character或不转换.zones。

#1


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:

1)州名单上没有$ time.population等级。这将创建一个NULL变量,由min处理,返回该警告消息。亲自尝试一下:

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:

2)(最有可能)变量look.at是一个数字(0),因为永远不会满足逻辑相等状态$ time.zone.1 == zone,所以它返回该值。自己检查一下:

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.

为了防止这两种情况,避免计算添加条件的这种向量的min,所以只有在!is.null(look.at)(第一点)和长度(look.at)!= 0(第二点)时才计算最小值)很满意。


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).

1)state $ population < - as.numeric(gsub(“\\,”,“”,state $ population))这可能会返回一个数字(0)。

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

2)另一个奇怪的事情,你转换为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:

但是,然后将原始数据(state $ time.zone.1)与转换为字符(.zone中的区域)进行比较,这绝对不是最安全的比较方式,如果发生错误转换,可能会导致不匹配:

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.

将state $ time.zone.1转换为character或不转换.zones。