使用spsample

时间:2016-05-18 20:49:12

标签: r sp

我正在尝试创建一个用于建模的网格。我已经设置了以下代码:

#--Grid Extents--#
xmin <- 1712352.4170
ymin <- 249753.9450
xmax <- 1714452.4170
ymax <- 252713.9450 

#--Set-up Grid--#
library(sp)
grd <- data.frame( x=c( xmin, xmax ), y=c( ymin, ymax ) )
coordinates( grd ) <- ~x+y
grd <- data.frame( spsample( grd , nsig=1, type='regular', cellsize=c( 20, 20 )))

为什么每次运行此代码(不进行任何更改)时,grd data.frame都有不同的值?

示例:运行1

head(grd)
   x1       x2
1 1712367 249760.5
2 1712387 249760.5
3 1712407 249760.5
4 1712427 249760.5
5 1712447 249760.5
6 1712467 249760.5

运行2:

head(grd)
   x1       x2
1 1712359 249764.2
2 1712379 249764.2
3 1712399 249764.2
4 1712419 249764.2
5 1712439 249764.2
6 1712459 249764.2

这是我的会话信息:

R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] nlme_3.1-118        knitr_1.9           rasterVis_0.28      hexbin_1.26.3       latticeExtra_0.6-26 RColorBrewer_1.0-5 
 [7] lattice_0.20-27     viridis_0.3.4         gstat_1.0-19        raster_2.2-12       rgeos_0.3-4        
[13] rgdal_0.9-1         sp_1.0-17           data.table_1.9.4   

loaded via a namespace (and not attached):
 [1] chron_2.3-45     colorspace_1.2-4 digest_0.6.8     evaluate_0.5.5   FNN_1.1          formatR_1.0      ggplot2_2.1.0   
 [8] gridExtra_2.2.1  gtable_0.1.2     htmltools_0.2.6  intervals_0.14.0 munsell_0.4.2    plyr_1.8.1       Rcpp_0.12.2     
[15] reshape2_1.4     rmarkdown_0.3.11 scales_0.4.0     spacetime_1.1-0  stringr_0.6.2    tools_3.1.3      xts_0.9-7       
[22] yaml_2.1.13      zoo_1.7-11

1 个答案:

答案 0 :(得分:1)

因为您正在随机抽样。尝试例如

set.seed(1)

在设置网格之前,您将获得相同的结果。