4

I want to create random points within polygons that are stored in a spatial data frame in R.

I tried to use the csr function of the splancs package, and the spsample of the sp package without success. I typed the following code:

Csr(Greece.map[1, ], 12) 

And received:

Error in storage.mode(poly) <- "double" no method for coercing this s4 class to a vector

And sample(Greece.map[1, ], n=12, random) receiving the following error:

Error in sample.Spatial(as(x, "Spatial"), n_tot * (1 + its * 0.1), type = type, : object random not found.

2

1 Answer 1

7

First we don't know what Greece.map is, so please try and use commonly available data for examples. I'll use the columbus spatial polygons obtained from library(spdep) and example(columbus).

Second, the syntax for sample is wrong:

sample(Greece.map[1, ], n=12, random) 

is wrong on two counts: it should probably be spsample, and random is a character string, not a bare word like that. For me:

> spsample(columbus,n=12,"random")
SpatialPoints:
              x        y
 [1,]  9.633657 12.18918
 [2,]  7.920523 11.79233
 [3,]  9.069013 12.94617
 [4,]  8.868344 12.45160
 [5,]  7.310185 11.46700

works perfectly, generating 12 points in the Columbus region.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.