删除错误的行

时间:2017-06-29 16:57:15

标签: r

我有一个带有坐标的数据框,我在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.更改操作的顺序给了我想要的结果,但我想知道如何处理这种情况并删除右行...你能给我一个提示吗?

3 个答案:

答案 0 :(得分: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)),]

或者这个(在R中你可以使用负指数来排除某些术语)

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), ]

答案 1 :(得分: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, ]

答案 2 :(得分: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, ]