我有一个简单的练习数据,当我尝试计算每个时区的最大,最小人口如下时,我收到了警告信息,如"在max(州$ time.population [look.at] ):没有max的非缺失参数;返回-Inf"。我试图通过手动更改"区域来逐个运行循环。每次他们都工作。我不确定那是什么原因。每个级别的区域都有空间,所以我想知道这是否是原因 - 我试图将其改为角色,但它仍然没有工作......任何人都知道如何解决这个问题?
state <- read.csv("states.csv")
state$population <- as.numeric(gsub("\\,","",state$population))
/ * the.zones&lt; - unique(state $ time.zone.1) the.zones&lt; - as.character(the.zones)* /
/ 新行 /
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
其他信息: 时区级别: 等级:AKST(UTC-09)CST(UTC-6)EST(UTC-5)HST(UTC-10)MT(UTC-07)PT(UTC-8) 如果更改为字符:&#34; CST(UTC-6)&#34; &#34; AKST(UTC-09)&#34; &#34; MT(UTC-07)&#34; &#34; PT(UTC-8)&#34; &#34; EST(UTC-5)&#34; &#34; HST(UTC-10)&#34;
数据是什么样的:
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)
答案 0 :(得分:1)
潜在的原因有两个:
1)$time.population
列表中没有state
级别。这将创建一个由NULL
处理的min
变量,并返回该警告消息。亲自尝试一下:
min(NULL)
2)(最有可能)变量look.at
是numeric(0)
,因为永远不会满足逻辑等式state$time.zone.1 == zone
,因此它返回该值。自己检查一下:
min(numeric(0))
要防止这两种情况,请避免计算添加条件的此类向量的min
,以便仅在!is.null(look.at)
(第一点)和length(look.at)!=0
(第二点)时计算最小值很满意。
编辑:还有其他一些问题可能导致问题:
1)state$population <- as.numeric(gsub("\\,","",state$population))
这可能会返回numeric(0)
。
2)另一件奇怪的事情,你在这里转换为character
:
the.zones <- unique(state$time.zone.1)
the.zones <- as.character(the.zones)
但是,您将原始数据(state$time.zone.1
)与转换为character
(zone in the.zone
)进行比较,这绝对不是最安全的比较方式,如果不匹配,可能会导致不匹配发生了错误的转换:
for(zone in the.zone){
look.at <- state$time.zone.1 == zone
...
}
将state$time.zone.1
转换为character
或不转换the.zones
。