简体   繁体   English

如何从 R 中的 shapefile 中提取特征样本?

[英]How do I take a sample of features from a shapefile in R?

I have a shapefile with thousands of features (called x).我有一个具有数千个特征的 shapefile(称为 x)。 I only want, say, 10% of those features.比如说,我只想要这些功能中的 10%。 How can I do a random sample of those features?如何对这些功能进行随机抽样? I've tried:我试过了:

    final_x <- sample(x,nrow(x)/10) 

but this returns an error.但这会返回错误。

I've also tried:我也试过:

    x_samp <- sample(x_samp$OBJECTID,nrow(x)/10)
    x_samp <- as.data.frame(x_samp)
    final_x <- x[x$OBJECTID==x_samp$x_samp]

I've managed to get what I want by:我设法通过以下方式获得了我想要的东西:

    x_samp <- sample(x$OBJECTID,nrow(x)/10)
    for (i in x_samp) {
    x1 <- x[x$OBJECTID==i,]
    final_x <- rbind(x,x1)
    }
     

The above just feels a little clunky and not very elegant.以上只是感觉有点笨拙,不是很优雅。 Is there a better solution?有更好的解决方案吗?

Many thanks非常感谢

Read your shapefile as an sf object, and then you can use bracketted subsetting.将您的 shapefile 读取为 sf object,然后您可以使用括号内的子集。

library(sf)

set.seed(432)
# reading example nc data as sf
my_sf <- read_sf(system.file('shape/nc.shp', package = 'sf'))
nrow(my_sf)
#> [1] 100

my_sf_sampled <- my_sf[sample(nrow(my_sf), size = nrow(my_sf)/10), ]
nrow(my_sf_sampled)
#> [1] 10

Created on 2021-02-26 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 2 月 26 日创建

Or, use dplyr::sample_frac :或者,使用dplyr::sample_frac

library(dplyr)
library(sf)

my_sf <- read_sf(system.file('shape/nc.shp', package = 'sf'))
sample_frac(my_sf, .1)

#> Simple feature collection with 10 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -81.9856 ymin: 34.82742 xmax: -76.02605 ymax: 36.54606
#> geographic CRS: NAD27
#> # A tibble: 10 x 15
#>     AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS  FIPSNO CRESS_ID BIR74 SID74 NWBIR74
#>  * <dbl>     <dbl> <dbl>   <dbl> <chr> <chr>  <dbl>    <int> <dbl> <dbl>   <dbl>
#>  1 0.104      1.55  2065    2065 Leno… 37107  37107       54  3589    10    1826
#>  2 0.154      1.68  2030    2030 Harn… 37085  37085       43  3776     6    1051
#>  3 0.172      1.84  2090    2090 Cumb… 37051  37051       26 20366    38    7043
#>  4 0.098      1.26  2097    2097 Hoke  37093  37093       47  1494     7     987
#>  5 0.078      1.38  2034    2034 Linc… 37109  37109       55  2216     8     302
#>  6 0.109      1.32  1841    1841 Pers… 37145  37145       73  1556     4     613
#>  7 0.18       2.14  1973    1973 Chat… 37037  37037       19  1646     2     591
#>  8 0.044      1.16  1887    1887 Chow… 37041  37041       21   751     1     368
#>  9 0.134      1.76  1958    1958 Burke 37023  37023       12  3573     5     326
#> 10 0.099      1.41  1963    1963 Tyrr… 37177  37177       89   248     0     116
#> # … with 4 more variables: BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>,
#> #   geometry <MULTIPOLYGON [°]>

Created on 2021-02-26 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 2 月 26 日创建

暂无
暂无

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

相关问题 R 如何合并具有多个多边形的 shapefile 中的多边形特征? (可重现的代码示例) - R How do I merge polygon features in a shapefile with many polygons? (reproducible code example) 如何将样本数据集从R包“spatstat”转换为shapefile - How to convert a sample dataset from the R package “spatstat” into a shapefile 如何将shapefile裁剪为R中的特定集? - How do I crop a shapefile to a specific set in R? 在 R 中,如何在不替换的情况下获取大小为 n 的样本,其中向量采样的长度为 &lt;= n - In R how do you take a sample of size n without replacement where length of vector sampling from is <= n 如何从R中的shapefile中按属性消除某些区域并创建新的shapefile? - How can I eliminate some areas by attribute from a shapefile in R and create a new shapefile? 如何根据R中的条件合并shapefile中包含的要素 - How to merge features contained in a shapefile based on a condition in R 如何从 R 中的流统计 package 中提取分水岭 shapefile? - How can I extract the watershed shapefile from the streamstats package in R? 如何从 R 数据帧的两列中联合采样? - How to take sample from two columns of R data frame jointly? 如何从 R 中的现有 shapefile 转换 crs 后保存 shapefile? - How to save a shapefile after transform the crs from the existing shapefile in R? 如何从 R 中的列中获取特定数字? - How do I take specific numbers from a column in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM