简体   繁体   中英

Subset random points from spatial points data frame

I need to select random points in R from a grid I created in ArcGIS. I'm sort of new to this so I'm not familiar with the codes.

I have a Large SpatialPointsDataFrame with 160831 elements (points) named "gridpts". I imported the points with "readOGR"

> names(gridpts)
[1] "gpts"   "L_code" "Lake"   "Area"

I want to subset the points by "L_code" and then select random points. This is what I have so far:

acr2.pts    <- gridpts[gridpts$L_code == "acr2",]
sample.acr2 <- sample(nrow(acr2.grid), 690)

However, this gives me a vector with the gpts and not a subset of the points which is what I want.

Thank you.

Hi Nicole and welcome to Stack Overflow. Please take some time and take a look at https://stackoverflow.com/help/how-to-ask for hints. It's a good start to give some data, make a How to make a great R reproducible example and give an example of your desired output.

Regarding your question, I make an assumption on your desired output. If me answer doesn't help you in solving your problem, I'll remove it.

Since you didn't provide any data, I just use the mtcars dataset. Furthermore I use the tidyverse -package.

library(tidyverse)
df <- mtcars

n <- 3 # sample size; in your case 690

First of all, I want to subset the dataset on a given condition and pull a sample of size n from these subset:

idx_sample <- df %>% 
                filter(cyl == 6) %>%  # in your case: L_code == "acr2"
                count() %>%           # count the datasets after subsetting; 7 in my case
                unlist %>%            # convert the tibble in to a vector  
                seq(1,.) %>%          # create a sequence, equal to 1:7 in my case
                sample(., n)          # get some (2 in my case) random indices

idx_sample contains the indizes of my sample of dataset after subsetting. Therefore

df %>%
  filter(cyl == 6) %>%                # subset again
  slice(idx_sample)                   # get the sampled data

gives us the sampled subset.

as Martin already mentioned it is not easy to help you without a working example. However i think i understand what you are trying to do, because i started with similar problems:

Spatialpointsdataframes work like regular dataframes, you can subset or select the rows and columns with square brackets eg [1:3,c(2,4,5)] for rows 1:3 of colums 2,4,5

after you already correctly subsetted your spdf to your desired subgroup:

acr2.pts <- gridpts[gridpts$L_code == "acr2",]

you need to subset again with your random samples:

sample.acr2 <- sample(nrow(acr2.pts), 690) (do you want to subset from the original number of rows or from the number of rows in your acr2 subset?)

this vector now contains the random row numbers, so you can select from the subset:

random_sub <- acr2.pts[sample.acr2,]

Theoretically you could also already put the sample function in there, but that might look chaotic:

random_sub <- acr2.pts[sample(nrow(acr2.pts), 690),]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM