1

I have been trying to create an empty SpatialPoints in R but have not found the way yet.

I have tried with this formula:

SpatialPoints(as.matrix(data.table(x = numeric(), y = numeric())))

But it didn't work. The closest thing I have found on the internet is Creating empty Polygons or SpatialPolygons in R? but that is for polygons and apparently it does not work the same way.

I have sp version 1.2.7

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 30, 2018 at 8:50
1
  • You should think about why you want to create this. If its to put in a for loop to incrementally add points to, you might be better off using an apply-type operation and then combining the results in the end - this is usually faster than growing a vector from an empty object. Commented Jul 30, 2018 at 13:40

2 Answers 2

2

Create a SpatialPoints object with one row in it, and then drop it with [-1,]:

> zero = SpatialPoints(data.frame(x = 0, y = 0))[-1,]
> zero
SpatialPoints:
 x y
Coordinate Reference System (CRS) arguments: NA 

Quick test to see what this thing is:

> length(zero)
[1] 0
> class(zero)
[1] "SpatialPoints"
attr(,"package")
[1] "sp"
answered Jul 30, 2018 at 13:36
1
1

Maybe there is a shorter way ...

new("SpatialPoints", 
 coords = structure(numeric(0), .Dim = c(0L, 2L), 
 .Dimnames = list(NULL, c("coords.x1", "coords.x2"))), 
 bbox = structure(c(1, 1, 1, 1), .Dim = c(2L, 2L), 
 .Dimnames = list(c("coords.x1", "coords.x2"),
 c("min", "max"))),
 proj4string = new("CRS", projargs = NA_character_)) 
# SpatialPoints:
# coords.x1 coords.x2
# Coordinate Reference System (CRS) arguments: NA 
answered Jul 30, 2018 at 9:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.