Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Edit your post to specify if your points are on a regular grid or if you need interpolation – Etiennebr Oct 8 '12 at 21:54

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([, 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([, c("x", "y", "z")])
  raster_cat[[i]] <- r
share|improve this answer
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, which does not rely on these internal names. – Paul Hiemstra Oct 15 '12 at 21:04
Good point. I've changed it. I like the slot usage because it's more compact, but you're right. – Etiennebr Oct 16 '12 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 <-
  r <- rasterFromXYZ(travel[, c("x", "y", "z")])
share|improve this answer
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 '13 at 14:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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