我有一个问题,我尝试了七个小时来解决,但没有任何成功。基本上,我试图从 openflights.org 可视化机场和航班数据。这本应该是一个简单、直接的可视化,但它已经变成了一个真正的拔毛器。
一切正常,直到 for 循环。当我尝试运行 for 循环以在我的地图上描绘飞行路径时,它运行了一点并且地图上出现了一些线条,但随后它退出并出现错误:
Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed
我试图做些什么来修复它:如您所见,我已经检查了数据集并删除了所有错误条目。例如,有一些 ID 是“\\N”,因此我完全删除了这些条目。然后我尝试将 ID 更改为数字而不是字符串,看看会发生什么。
在我运行 for 循环后它总是在同一时间出错,有没有办法可以查看它返回错误时到达的行?我也是新人R。
我知道以前有人问过类似的问题,但他们那里的解决方案都没有对我有用。感谢您提供任何帮助。
代码:
library('geosphere')
library('maps')
# Reading in the files from openflights.org
airports <- read.csv("airports.dat.txt", header=FALSE, col.names=c("Airport ID","Name","City","Country","IATA","ICAO","Latitude","Longitude",
"Altitude","Timezone","DST", "TZ Database","Type","Source"), as.is=TRUE)
flights <- read.csv("routes.dat.txt", header=FALSE, col.names=c("Airline","Airline ID","Source Airport","Source Airport ID","Destination Airport",
"Destination Airport ID","Codeshare","Stops","Equipment"), as.is=TRUE)
# Cleaning the data set, there are some instances of the value "\\N" in the flights data set.
flights <- flights[!grepl("\\N", flights$Source.Airport.ID),]
flights <- flights[!grepl("\\N", flights$Destination.Airport.ID),]
# Converting all of the IDs to numbers (I thought this might work but it did not)
flights$Source.Airport.ID <- as.numeric(flights$Source.Airport.ID)
flights$Destination.Airport.ID <- as.numeric(flights$Destination.Airport.ID)
airports$Airport.ID <- as.numeric(airports$Airport.ID)
# Creating a world background
map("world", col="white", border="gray10", fill=TRUE, bg="gray30")
# Adding all of the airports as points
points(x=airports$Longitude, y=airports$Latitude, pch=19,
cex=0.05, col="blue")
# Generating a color set to be used for mapping the flight paths
col.1 <- adjustcolor("limegreen", alpha=0.05)
col.2 <- adjustcolor("darkgreen", alpha=0.05)
edge.pal <- colorRampPalette(c(col.1, col.2), alpha = TRUE)
edge.col <- edge.pal(100)
# Now, generating the visualization of the flight paths.
# Here is where the error occurs, when I run this.
# It gets through some of the data but then errors out.
for(i in 1:nrow(flights)) {
node1 <- airports[airports$Airport.ID == flights[i,]$Source.Airport.ID,]
node2 <- airports[airports$Airport.ID == flights[i,]$Destination.Airport.ID,]
arc <- gcIntermediate( c(as.numeric(node1[1,]$Longitude), as.numeric(node1[1,]$Latitude)),
c(as.numeric(node2[1,]$Longitude), as.numeric(node2[1,]$Latitude)),
n=1000, addStartEnd=TRUE)
#edge.ind <- round(100*table(flights[i,]$Source.Airport.ID) / table(max(airports$Airport.ID)))
#lines(arc, col=edge.col[edge.ind], lwd=edge.ind/30)
lines(arc, col = "limegreen", lwd = 0.02)
}