2017-06-29 64 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, ]