【问题标题】:Merge: ‘NA’, ‘NA’ are duplicated in the result合并:‘NA’、‘NA’在结果中重复
【发布时间】:2018-03-31 01:16:40
【问题描述】:

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

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 = "ID", by.y = "sourceAirportID") : 列名 ‘NA’, ‘NA’ 是 在结果中重复

警告信息: 在 merge.data.frame(airports,arrivals, by.x = "ID", by.y = "destinationAirportID") : 列名‘NA’、‘NA’在 结果

我做错了什么?

【问题讨论】:

    标签: r


    【解决方案1】:

    首先,这些只是警告,而不是错误。也就是说,一切仍然有效,但可能不如预期。问题出在

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

    您只指定了 11 个列名,而有 14 个列。由于这个原因,三列被命名为NA,这对merge来说是一个潜在的问题。

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

    【讨论】:

    • 啊,是的!那讲得通。我做了你推荐的改变,现在一切正常。一如既往,我试图自己解决这个问题,所以我不必问一个愚蠢的问题,但这个问题让我回避了。再次感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多