2017-06-29 20 views
0

私はSpatialPointsで変換された座標系を持つデータフレームを持っていて、太平洋と太平洋に落ちるものがあることに気づいています。間違った行を削除する

だから私は、Googleマップの座標を見て、太平洋にあったどのポイントを見つけるために、そして大西洋のもの

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

を除去するためにこれをしなかった:

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

私はブラジルのすべてを除外し、2番目のブラジルのみを使うとメキシコのいくつかのポイントを除外するため、この2つの条件が必要でした。

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 

最初の数字は行番号、2番目はマイコード、緯度と経度が続きます。

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

しかし、このコードは、緯度が0であった点を除去し、経度はなかった-99:(二つが最初海ではなく、小さな島であるため)、私は最後の7行を除外しなければなりません

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

新しいオブジェクト、data_arvore2がdata_arvore未満の7行を持っています...しかし、ポイントはまだ海に落ちた:経度は-99であり、緯度が0新しい試みなかった、また、点...コードが間違った行を削除したことに気付きました。それから、私はもう一度始めましたが、太平洋の地点を取り除く前に太平洋の列を取り除き、それがうまくいったのです。私は最初の試みで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)),] 

またはこの(Rであなたには、いくつかの条件を除外するために、負のインデックスを使用することができます)

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

2回目の試行では、行番号を行番号と混同していましたが、最初の試行では同じでしたサブセット化して並べ替えると、それ以上はなくなります。

私はこのような何かが働いていると思います:

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

すべてのオプションが機能し、R構文について多くのことを学びました。 – Thai

+0

喜んで助けてください:)! –

+0

ところで、私はちょっと速いと答えたのでちょうど気づいたが、最初の3つの解決策では必要ないが、インデックスの代わりにブール値の配列を与えるとうまくいく。 –

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

こんにちは、Matt ...ありがとうあなたの助けを借りて...私はこの可能性に非常に興奮していました...それは私を助けてくれるでしょう...しかしそれはうまくいきませんでした...私は次のようになります...> pts < - SpatialPoints(data_arvore [、c(関数(classes、fdef、mtable)のエラー:関数 'proj4string'の継承されたメソッドを署名 ''文字 ''のために見つけることができません( "latitude"、 "longitude"))、proj4string = CRS(proj4string(wrld_simpl) ... – Thai

1

私の前の回答は、しかしmaptoolsパッケージのグローバルマップが小さな島に落ちるポイントをキャプチャするために必要な詳細レベルを持っていない、一般的な使用のためによかったです。以下のコードは、gadm.orgからより詳細なレベルのマップを取得するために使用できます。

使用する地図のサイズがはるかに大きいため、データセット内の国の地図のみを含めることをおすすめします。この例では、コスタリカ、ニカラグア、パナマのための国別マップを掲載しています。

最初にこのスクリプトを実行すると、必要な国マップを1つのファイルにダウンロードして組み合わせることができます。

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, ] 
関連する問題