我有一个简单的数据供练习,当我试图计算每个时区的最大、最小人口时,我收到类似于"In max(state$time.populationlook.at):不丢失参数给max;返回-Inf“之类的警告消息。我试图一个一个地运行循环,每次都手动更改“区域”,它们都起作用了。我不知道原因是什么。每个级别的区域都有空格,所以我想知道这是否是原因--我试图将其更改为字符,但它仍然不知道如何修复这个问题?
state <- read.csv("states.csv")
state$population <- as.numeric(gsub("\\,","",state$population))/* the.zones <-唯一(状态为$time.zone.1) the.zones <- as.character(the.zones)*/
/New线路/
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)如果更改为字符:"CST (UTC-6)“"AKST (UTC-09) "MT (UTC-07)”PT (UTC-8)“"EST (UTC-5)”"HST (UTC-10)“
这些数据是什么样子的:
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)发布于 2014-10-11 23:47:16
潜在的原因有两个:
1)在$time.population列表中没有state级别。这将创建一个由NULL处理的min变量,并返回该警告消息。你自己试试吧:
min(NULL)(最有可能是)变量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。
https://stackoverflow.com/questions/26320310
复制相似问题