合并:'NA','NA'在结果中重复

时间:2018-03-30 16:59:25

标签: r

我正在查看此链接中的示例代码。

http://www.milanor.net/blog/maps-in-r-plotting-data-points-on-a-map/

似乎数据集位置有变化,所以我稍微改变了我的代码以适应。

airports <- read.delim("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", sep=",")
colnames(airports) <- c("ID", "name", "city", "country", "IATA_FAA", "ICAO", "lat", "lon", "altitude", "timezone", "DST")
head(airports)

library(rworldmap)
newmap <- getMap(resolution = "low")
plot(newmap, xlim = c(-20, 59), ylim = c(35, 71), asp = 1)
points(airports$lon, airports$lat, col = "red", cex = .6)



routes <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat", header=F)
colnames(routes) <- c("airline", "airlineID", "sourceAirport", "sourceAirportID", "destinationAirport", "destinationAirportID", "codeshare", "stops", "equipment")
head(routes)

library(plyr)
departures <- ddply(routes, .(sourceAirportID), "nrow")
names(departures)[2] <- "flights"
arrivals <- ddply(routes, .(destinationAirportID), "nrow")
names(arrivals)[2] <- "flights"

所有这一切似乎都很好。下面的代码似乎已关闭,但我不确定原因。

airportD <- merge(airports, departures, by.x = "ID", by.y = "sourceAirportID")
airportA <- merge(airports, arrivals, by.x = "ID", by.y = "destinationAirportID")

我收到此错误:

  

警告讯息:   在merge.data.frame(机场,离境,by.x =   &#34; ID&#34;,by.y =&#34; sourceAirportID&#34;):列名'NA','NA'是   在结果中重复

     

警告讯息:   在merge.data.frame(机场,到达,by.x =&#34; ID&#34;,by.y =   &#34; destinationAirportID&#34;):列名称'NA','NA'重复   结果

我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,这些只是警告,而不是错误。也就是说,一切仍然有效,但也许不是预期的。问题来自

colnames(airports) <- c("ID", "name", "city", "country", "IATA_FAA", "ICAO", "lat", "lon", "altitude", "timezone", "DST")

您只指定了11个列名,而有14列。因此,三列名为NA,这是merge的潜在问题。

如果结果(即airportDairportA)符合预期,则无需更改任何内容。不过,建议为这三列提供正确的列名。

相关问题