简体   繁体   English

来自空间点数据帧的子集随机点

[英]Subset random points from spatial points data frame

I need to select random points in R from a grid I created in ArcGIS.我需要从我在 ArcGIS 中创建的网格中的 R 中的 select 随机点。 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".我有一个包含 160831 个元素(点)的大型 SpatialPointsDataFrame,名为“gridpts”。 I imported the points with "readOGR"我用“readOGR”导入了点

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

I want to subset the points by "L_code" and then select random points.我想通过“L_code”对点进行子集化,然后是 select 随机点。 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.但是,这给了我一个带有 gpts 的向量,而不是我想要的点的子集。

Thank you.谢谢你。

Hi Nicole and welcome to Stack Overflow.嗨,妮可,欢迎来到 Stack Overflow。 Please take some time and take a look at https://stackoverflow.com/help/how-to-ask for hints.请花点时间查看https://stackoverflow.com/help/how-to-ask以获取提示。 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.提供一些数据是一个好的开始,制作如何制作出色的 R 可重现示例并给出您想要的 output 的示例。

Regarding your question, I make an assumption on your desired output.关于您的问题,我假设您想要的 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.由于您没有提供任何数据,我只使用mtcars数据集。 Furthermore I use the tidyverse -package.此外,我使用tidyverse

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:首先,我想在给定条件下对数据集进行子集化,并从这些子集中提取大小为n的样本:

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. idx_sample包含子集后我的数据集样本的 indizes。 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 Spatialpointsdataframes 像常规数据帧一样工作,您可以子集或 select 带有方括号的行和列,例如 [1:3,c(2,4,5)] 用于列 2,4,5 的行 1:3

after you already correctly subsetted your spdf to your desired subgroup:在您已经将您的 spdf 正确子集到所需的子组之后:

acr2.pts <- gridpts[gridpts$L_code == "acr2",] 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?) sample.acr2 <- sample(nrow(acr2.pts), 690) (您想从原始行数还是从 acr2 子集中的行数中进行子集化?)

this vector now contains the random row numbers, so you can select from the subset:这个向量现在包含随机行号,所以你可以从子集中 select :

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

Theoretically you could also already put the sample function in there, but that might look chaotic:从理论上讲,您也可以将样本 function 放在那里,但这可能看起来很混乱:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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