min和max函数没有非缺失参数

3

我在使用R语言编写下面的代码,在地图上绘制了一些坐标点:

library(ggplot2)
library(ggmap)

smalloperations <- read.csv("S:/smalloperations.csv", na.strings=c("","NA"))

lon <- c(smalloperations$target.longitude)
lat <- c(smalloperations$target.latitude)
df <- as.data.frame(cbind(lon,lat))

# getting the map
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), 
zoom = 1, maptype = "satellite", scale = 2)

# plotting the map with some points on it
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size 
= 5, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)

当我运行此代码时,出现以下警告信息:
警告消息: 1:在min(x)中:没有非缺失参数;返回Inf 2:在max(x)中:没有非缺失参数;返回-Inf 3:在min(x)中:没有非缺失参数;返回Inf 4:在max(x)中:没有非缺失参数;返回-Inf
然后该代码显示坐标,但没有背景地图。我已经查看了一些其他人遇到的这个错误,但我看到的解决方案似乎不适合我的具体问题。 我使用的是较小、清理过的 Kaggle数据集的版本,其中目标经度和纬度中没有空值或NA,整个数据集中也没有NA。
我的数据结构:
'data.frame':   13 obs. of  20 variables:
$ mission.id                 : int  1 2 3 4 5 6 7 8 9 10 ...
$ mission.date               : Factor w/ 1 level "15/08/1943": 1 1 1 1 1 1 1 
1 1 1 ...
$ theater.of.operations      : Factor w/ 4 levels "CBI","ETO","MTO",..: 3 4 
3 3 4 4 4 4 3 3 ...
$ country                    : Factor w/ 2 levels "GREAT BRITAIN",..: 2 2 2 
2 2 2 2 2 2 2 ...
$ air.force                  : Factor w/ 4 levels "10 AF","12 AF",..: 2 3 2 
2 3 3 3 3 2 2 ...
$ aircraft.series            : Factor w/ 4 levels "A36","B17","B24",..: 1 3 
1 1 3 3 3 2 1 1 ...
$ mission.type               : Factor w/ 4 levels "OBJECTIVE BOMBING",..: 4 
1 4 4 1 1 3 1 4 4 ...
$ takeoff.base               : Factor w/ 2 levels "PONTE OLIVO AIRFIELD",..: 
1 2 1 1 2 2 2 2 1 1 ...
$ takeoff.location           : Factor w/ 2 levels "SICILY","UNKNOWN": 1 2 1 
1 2 2 2 2 1 1 ...
$ target.id                  : Factor w/ 6 levels "16140","3735",..: 4 6 5 1 
6 6 6 6 3 1 ...
$ target.country             : Factor w/ 5 levels "BURMA","GERMANY",..: 3 4 
3 3 5 4 4 4 3 3 ...
$ target.city.or.area        : Factor w/ 10 levels "BERLIN","COSENZA",..: 10 
6 2 3 5 4 8 8 9 3 ...
$ target.type                : Factor w/ 5 levels "AIRDROME","CITY AREA",..: 
4 3 4 4 5 4 4 1 4 4 ...
$ target.industry            : Factor w/ 3 levels "ARMAMENT AND ORDNANCE 
PLANTS",..: 3 3 3 3 3 3 3 3 3 1 ...
$ target.priority            : int  9 1 9 9 1 1 1 1 9 9 ...
$ target.latitude            : num  38.22 -7.17 39.27 38.43 -1.12 ...
$ target.longitude           : num  15.4 147 16.2 15.9 103.9 ...
$ altitude..hundreds.of.feet.: int  139 44 139 139 60 35 70 40 139 139 ...
$ source.id                  : Factor w/ 9 levels "11287","11326",..: 9 8 9 
9 3 7 1 2 9 9 ...
$ bomb.type                  : Factor w/ 2 levels "HIGH EXPLOSIVES",..: 1 1 
1 1 1 1 1 1 1 1 ...

哪一行代码抛出了错误?该代码并没有明确涉及minmax - John Coleman
在最后四行之后我遇到了错误,是的,我在想也许我必须在某个地方包含它们,但我不知道我应该在哪里这样做。 - Niall
2
你的问题离一个 [mcve] 还有很大的距离,实在很难说什么。某个函数(你并没有具体说明是哪个)不喜欢你输入的某些内容(而我们也不知道这些内容是什么)。我们应该猜测问题出在哪里吗? - John Coleman
1
阅读这篇文章可能会有帮助:https://dev59.com/eG025IYBdhLWcg3whGSx。 - John Coleman
由于您没有向我们展示您的数据 - 至少请显示您数据框的结构`str(df)`然后以更合适的方式设置您的数据框,例如,`df <- data.frame(lon = smalloperations$target.longitude, lat = smalloperations$target.latitude)`看看是否有不同的数据结构您的smalloperations数据中是否有任何NULL值?如果是,则需要使用mean(df$lon, na.rm=T)。或者分享您的数据,有人可能可以明确回答这个问题。 - B Williams
我在那里添加了更多信息,并包含了我的数据结构。由于列数较多,当我尝试显示我的数据时,它变得有点混乱,但其中没有NA或NULL值,我用UNKNOWNS替换了它们或使用平均值填充了它们。 - Niall
2个回答

0

1
这不是问题的答案,因此应该作为评论添加。 - Joris Meys
但是,如果数据中存在NA,则说明为什么没有地图。有点像鸡和蛋的问题,不是吗? - B Williams
我只是表达了 Stack Overflow 的政策。请参阅:http://stackoverflow.com/help/how-to-answer,特别是关于如何回答良好提出的问题的部分。您的回答在审核堆栈中被标记为“不是答案”,我只是想通知您。我并没有制定这里的政策;-) - Joris Meys
1
啊 - 很有道理。改变了答案。 - B Williams

-1

没有可重现的答案,所以我不能确定。但是在R中的hablar包中(可能)有一个解决方案。mean函数在处理NA、Inf时效果不佳。在lat和lon上添加s函数可以得到:

library(ggplot2)
library(ggmap)
library(hablar)

smalloperations <- read.csv("S:/smalloperations.csv", na.strings=c("","NA"))

lon <- c(smalloperations$target.longitude)
lat <- c(smalloperations$target.latitude)
df <- as.data.frame(cbind(lon,lat))

# getting the map
mapgilbert <- get_map(location = c(lon = mean(s(df$lon)), lat = mean(s(df$lat))), 
zoom = 1, maptype = "satellite", scale = 2)

# plotting the map with some points on it
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size 
= 5, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接