There are 100 SpatialPointsDataFrames in my Workspace. I loaded them on this way:

filenames <- list.files(path="",

for(i in filenames){
              filepath <- file.path("/",i)
              assign(i, readShapePoints(filepath))

They are called like this:



How can I convert them to rasters looping through the Workspace?

I am very new R user and hoping to find some help. thanks a lot.

  • 1
    Edit your post to specify if your points are on a regular grid or if you need interpolation Oct 8, 2012 at 21:54

2 Answers 2


If your data contains xyz data (where z is the raster value) and your points are on a regular grid (no need for interpolation).

r <- rasterFromXYZ(as.data.frame(travel)[, c("x", "y", "z")])

If you need interpolation, you can use akima library :


steps <- 100
isu <- with(travel@data, interp(x, y, z, 
    xo=seq(min(x), max(x), length = steps),
    yo=seq(min(y), max(y), length = steps)

r <- raster(isu)

Now, to do this sequentially you just need to wrap it in a for loop (I've tried to stay as close as possible from the information you gave in your question) :


filenames <- list.files(path="", pattern="XYhectareTravelTimes_ez+.*shp")

# create a container for all the rasters
raster_cat <- list()

for (i in filenames) { 
  travel <- readShapePoints(i)
  r <- rasterFromXYZ(as.data.frame(travel)[, c("x", "y", "z")])
  raster_cat[[i]] <- r
  • I'd advice against using travel@data (or using @ at all), as this relies on the internal names of a SpatialPointsDataFrame, which might change. I suggest using as.data.frame, which does not rely on these internal names. Oct 15, 2012 at 21:04
  • Good point. I've changed it. I like the slot usage because it's more compact, but you're right. Oct 16, 2012 at 14:09

In addition to @Etiennebr's answer, I'd go for an apply style loop (which is more R-ish, and uses less code for the same thing):


filenames <- list.files(path="", pattern="XYhectareTravelTimes_ez+.*shp")

raster_cat = lapply(filenames, function(x) {
  travel <- as.data.frame(readShapePoints(x))
  r <- rasterFromXYZ(travel[, c("x", "y", "z")])
  • Hoi Paul, thank you very much for the answer!! Your method is much easyer than the one I figured out. If you are interested in my methode just let me know. Best regards, Livia
    – user14723
    Jan 30, 2013 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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