I need to create and SpatialLinesDataFrame with a predefined structure (9 fields). I'm performing a function over a list of shp which I want to append to this empty spatiallinesdataframe.

I already know how to save each shp within a folder and then merge all, but now, I'd like to merge all the sph within the iteration to avoid save and reread steps.

1 Answer 1


I'd use this trick, which is to create a 1-row SLDF with the right attributes, and then drop the first and only row, leaving you with nothing:

> library(sp)
> library(raster)

Here's the beef:

> s = SpatialLinesDataFrame(

Set up a one-line data frame with your attributes in the last line - here I've got three. The finale [-1,] drops the one row created, leaving you with:

> s
class       : SpatialLinesDataFrame 
features    : 0 
coord. ref. : NA 
variables   : 3
names       :    a,    b,    c 

A SLDF with no rows (features) and 3 variables.

  • This is exactly what I was looking for. Thanks a lot ¡ Commented Jan 2, 2019 at 13:17

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.