2017-06-29 65 views
0

我有一个坐标数据帧,我在SpatialPoints中转换,注意到有些会落在太平洋和一些在大西洋。删除错误的行

所以我看着谷歌地图坐标,这样做是为了删除那些在大西洋

data_arvore<- data_arvore[which(data_arvore$longitude < -34.8),] 

然后,找出哪些点是在太平洋:

data_arvore[which(data_arvore$latitude < 7 & data_arvore$longitude < -82),] 

我需要这两个条件,因为如果我只使用第一个条件,它将排除巴西的所有事情,如果我只使用第二个条件,它将排除墨西哥的一些点。我得到这个打印:

3007 GB19507  5.550000 -87.03000 

4085 GB943  0.000000 -99.00000 

4086 GB942  0.000000 -99.00000 

4087 GB940  0.000000 -99.00000 

6672 GB4718  0.000000 -99.00000 

7282 GB5497  0.000000 -99.00000 

7283 GB5496  0.000000 -99.00000 

10354 GB12229  0.000000 -99.00000 

第一个数字是行号,第二个是我的代码,其次是经度和纬度。我必须排除最后7行(因为前两个是不是在海洋中,而是在一个小岛):

data_arvore2<- data_arvore[which(data_arvore$latitude != 0 & data_arvore$longitude != -99),] 

但这个代码删除其纬度为0分,东经不-99 ,并指出其经度为-99和纬度是不为0的新尝试:

data_arvore2 <- data_arvore[-c(4085,4086,4087,6672,7282,7283,10354), ] 

新的对象,data_arvore2比data_arvore少7行......但点在海洋仍在下降...我注意到代码删除了错误的行。然后我又开始了,但是在去除大西洋中的点之前,删除了太平洋中的行,并且它工作正常。我发现在第一次尝试时我删除了第4086行,这不是行号4086.改变操作的顺序给了我期望的结果,但是我想知道如何处理这种情况并删除正确的行......你能给我一个小费吗?

回答

2

你想要一个OR代替AND:

data_arvore2<- data_arvore[which(data_arvore$latitude != 0 | data_arvore$longitude != -99),] 

你也可以这样做:

data_arvore2<- data_arvore[which(!(data_arvore$latitude == 0 & data_arvore$longitude == -99)),] 

或者,这(在读你可以使用负的索引以排除一些术语)

data_arvore2<- data_arvore[-which(data_arvore$latitude == 0 & data_arvore$longitude == -99),] 

在第二次尝试中,您将行号混淆了行号,它们在初始化初始值时是相同的表,但经过子集和重新排序后,它们不再存在。

我觉得这样的事情会工作:

data_arvore2 <- data_arvore[-match(as.character(c(4085,4086,4087,6672,7282,7283,10354)),row.names(data_arvore), ] 
+0

所有的选项都有效,我学到了很多关于R语法的知识! – Thai

+0

高兴地帮助:)! –

+0

顺便提一下,我刚刚注意到了,因为我的回答有点快,但是你不需要在第一个解决方案中的哪一个,它可以很好地给出一组布尔值而不是索引 –

0

这是利用maptools包的脚本。您可以提供原始data_arvore数据帧,并且应该删除掉落海洋的任何位置。

library(maptools) 

# Load the wrld_simpl polygon 
data(wrld_simpl) 

# Map the locations from data_arvore 
pts <- SpatialPoints(data_arvore[,c("longitude","latitude")], proj4string=CRS(proj4string(wrld_simpl))) 

# Find which points fall on oceans 
data_arvore$ocean <- is.na(over(pts, wrld_simpl)$FIPS) 

# Not necessary, but will generate a map showing locations 
plot(wrld_simpl) 
points(pts, col = 3 - data_arvore$ocean, pch=16) 

# Remove locations that fall on oceans 
data_arvore <- data_arvore[data_arvore$ocean == FALSE, ] 
+0

嗨,马特......谢谢为你的帮助......我对这种可能性感到非常兴奋......它会帮助我很多...但它没有工作......我得到以下...> pts < - SpatialPoints(data_arvore [,c( (函数(类,fdef,mtable)中的错误:无法找到函数'proj4string'进行签名的继承方法''字符'' ... – Thai

1

我以前的答案是很好的一般用途,但是在maptools包的全球地图没有细节的捕捉落在小岛屿上的点所需要的水平。以下代码可用于从gadm.org获取具有更高级别细节的地图。

使用的地图尺寸要大得多,因此建议您只包含位于数据集内的国家/地区的地图。在这个例子中,我包括哥斯达黎加,尼加拉瓜和巴拿马的国家地图。

首先运行这个脚本,以便将您需要的国家地图下载并合并到一个文件中。

library(rgdal) 
library(prevR) 

#### Download and combine map shapefiles for required countries #### 
#### This section only needs to be run one time in order to create a single map file with all countries needed #### 
#### These map files will be downloaded from http://www.gadm.org/ #### 

    # Create vector of countries to obtain maps for. Use only the three letter country abbreviation 
    # Country code abbreviations are available at this website http://www.nationsonline.org/oneworld/country_code_list.htm 
    countries <- c("CRI","NIC","PAN") 

    # Create a temporary working folder 
    tempfldr <- tempdir() 

    # Set paths for temporary folders for zip files, unzipped maps, and final map 
    map.zips <- file.path(tempfldr, "mapzips") 
    maps.fldr <- file.path(tempfldr, "maps") 
    final.fldr <- file.path(tempfldr, "final") # Probably set this to a location where it can be permanently stored 

    # Create temporary folders 
    if(dir.exists(map.zips) == FALSE){ 
    dir.create(map.zips) 
    } 
    if(dir.exists(maps.fldr) == FALSE){ 
    dir.create(maps.fldr) 
    } 
    if(dir.exists(final.fldr) == FALSE){ 
    dir.create(final.fldr) 
    } 

    # Download each countries map file 
    sapply(countries, function(x) download.file(paste0("http://biogeo.ucdavis.edu/data/gadm2.8/shp/",x,"_adm_shp.zip"), file.path(map.zips,paste0(x,".zip")))) 

    # Extract contents of zip files 
    sapply(unlist(list.files(map.zips, full.names = TRUE)), unzip, exdir = maps.fldr) 

    # Get list of shapefiles to be used 
    shapefiles <- unlist(list.files(maps.fldr, pattern = "0.shp", full.names = TRUE)) 

    # Read all shapefiles 
    shapefiles <- lapply(shapefiles, readOGR) 

    # Combine all shapefiles into a single object 
    final.map <- do.call(rbind, shapefiles) 

    # Save the final combined map for later use 
    writeOGR(obj = final.map, dsn = final.fldr, layer = "final.map", driver = "ESRI Shapefile") 

组合国家地图创建后。您可以使用此脚本来使用新创建的地图来检查您的数据集。

# Dataframe with coordinates to check 
    data_arvore <- data.frame(latitude = c(5.537175, 11.618371, rep(0,8)), 
          longitude = c(-87.052112, -85.365203, rep(-99,8))) 

    # Read in the map file created eariler 
    map <- readOGR(file.path(final.fldr, "final.map.shp")) 

    # Get points from dataframe on map 
    pts <- SpatialPoints(data_arvore[,c("longitude","latitude")], proj4string=CRS(proj4string(map))) 

    # Check which points are over ocean 
    data_arvore$ocean <- is.na(over(pts, map)$FIPS) 

    # Create a map for verification 
    plot(map) 
    points(pts, col = 3 - data_arvore$ocean, pch=16) 

    # Remove points that are over ocean 
    data_arvore <- data_arvore[data_arvore$ocean == FALSE, ]