R 如何从反向地理代码函数中获取ISO3代码,而不是因子索引?

R 如何从反向地理代码函数中获取ISO3代码,而不是因子索引?,r,iso,reverse-geocoding,country-codes,rworldmap,R,Iso,Reverse Geocoding,Country Codes,Rworldmap,我正在处理一个超过270000个观察值的三变量。这三个变量是观测值、纬度和经度。在之前的一篇相关文章中,我设法获得了有关如何允许反向地理编码功能跳过缺少纬度和经度值的观测的帮助: 可复制示例: Data <- data.frame( Observation = 1:5, Longitude = c(116.3880005, 53, -97, NA, NA), Latitude = c(39.92890167, 32, 32, NA, NA)) 我想知道我可以修改上面的代码,

我正在处理一个超过270000个观察值的三变量。这三个变量是观测值、纬度和经度。在之前的一篇相关文章中,我设法获得了有关如何允许反向地理编码功能跳过缺少纬度和经度值的观测的帮助:

可复制示例:

Data <- data.frame(
  Observation = 1:5,
  Longitude = c(116.3880005, 53, -97, NA, NA), 
  Latitude = c(39.92890167, 32, 32, NA, NA))

我想知道我可以修改上面的代码,以便输出ISO3代码,而不是它们的因子索引

我觉得这一切都很好:

> pts=read.table("points.csv",head=TRUE,sep=",")
> pts=subset(pts,!is.na(lon)) # just take the good ones to save NA faffing
> coordinates(pts)=~lon+lat
> first = pts[1:100,]         # take first 100 for starters
> cc = coords2country(first)
> plot(first,pch=".")
> text(coordinates(first),label=cc)


所有国家都在正确的位置…

我认为您的主要问题是,您输出的是每个ISO3代码的因子索引,而不是ISO3代码本身。因此,中国有42个,因为中国是地图上的第42个国家。下面的as.character()对其进行排序

因此,对你的&Barry的代码进行一些小的编辑,我认为下面的代码就是你想要的

将最后4行中的“first”替换为“pts”,以便为整个数据集运行

coords2country = function(points)
{  
  library(rworldmap)
  countriesSP <- getMap(resolution='low')

  #I assume that points have same projection as the map
  pointsSP = SpatialPoints(points, proj4string=CRS(proj4string(countriesSP)))  

  # use 'over' to get indices of the Polygons object containing each point 
  indices = over(pointsSP, countriesSP)

  #as.character(indices$ADMIN) # return the ADMIN names of each country  
  as.character(indices$ISO3) # return the ISO3 code
  #as.character(indices$ISO_N3) # return the ISO numeric code
}


library(sp)
pts=read.table("points.csv",head=TRUE,sep=",")
pts=subset(pts,!is.na(lon)) # just take the good ones to save NA faffing
coordinates(pts)=~lon+lat
first = pts[1:100,]         # take first 100 for starters
cc = coords2country(first)
plot(first,pch=".")
text(coordinates(first),label=cc)

firstPlusISO3 <- cbind(coordinates(first),cc)  
coords2country=功能(点数)
{  
图书馆(世界地图)

CountriesP您是如何读取该点文件并将其传递给函数的?为什么不在此处简单地包含前10个点,并向我们展示您获得的内容,以及您希望获得的国家/地区?您确定您没有将lat和long up混为一谈吗?“超过27000”这是一种轻描淡写的说法。该数据文件中有257388行。您好,我为观察数量的输入错误表示歉意。我确信我没有混淆lat和long。我试图完成的是在csv文件中插入一个新列,其中包含每个观察的相应ISO3代码。缺少数据时使用NA?Y你真的需要向我们展示一些点的记录,你得到了什么,以及它们是如何错误的。我现在在csv文件的第一行提供了一个示例,说明了我得到的输出(就产生的ISO3值而言)有多错误。感谢你提供绘图代码。但是,我的最终目标是在我的csv文件中创建一个新列,其中包含ISO3 c每一次观察的颂歌。
> pts=read.table("points.csv",head=TRUE,sep=",")
> pts=subset(pts,!is.na(lon)) # just take the good ones to save NA faffing
> coordinates(pts)=~lon+lat
> first = pts[1:100,]         # take first 100 for starters
> cc = coords2country(first)
> plot(first,pch=".")
> text(coordinates(first),label=cc)
coords2country = function(points)
{  
  library(rworldmap)
  countriesSP <- getMap(resolution='low')

  #I assume that points have same projection as the map
  pointsSP = SpatialPoints(points, proj4string=CRS(proj4string(countriesSP)))  

  # use 'over' to get indices of the Polygons object containing each point 
  indices = over(pointsSP, countriesSP)

  #as.character(indices$ADMIN) # return the ADMIN names of each country  
  as.character(indices$ISO3) # return the ISO3 code
  #as.character(indices$ISO_N3) # return the ISO numeric code
}


library(sp)
pts=read.table("points.csv",head=TRUE,sep=",")
pts=subset(pts,!is.na(lon)) # just take the good ones to save NA faffing
coordinates(pts)=~lon+lat
first = pts[1:100,]         # take first 100 for starters
cc = coords2country(first)
plot(first,pch=".")
text(coordinates(first),label=cc)

firstPlusISO3 <- cbind(coordinates(first),cc)