[R] Varying name of output tables from looped process of list of spdf objects

Larrosa, Cecilia cecilia.larrosa10 at imperial.ac.uk
Sun Jul 26 21:06:38 CEST 2015


Hi,

This is a repost from here (http://r.789695.n4.nabble.com/Writing-output-of-a-looped-process-with-pdfs-tt4710348.html), due to the post not being complete originally. I am running R studio on OS X Yosemite 10.10.4 (Mac). I appreciate you help very much!

The objective: I have 100 shapefiles that need to undergo the same process.

The process: I use gDistance{rgdal} to calculate the distance between all features (polygons) within each layer, and output a txt file.

The problem: I need the name of the output txt file to contain the name of the shapefile, but the shapefiles are read into R as SpatialPolygonsDataFrames (spdf) and I cannot find a way to use the name of the spdf objects as character in order to make it vary with each iteration.

My questions to you: Do you know a way to solve the problem or an alternative way to fulfil the objective? I have come to determine the problem after searching about the error message, have I interpreted correctly?


Here is a minimal dataset for replicability:

> dput(a_1)
new("SpatialPolygonsDataFrame"
    , data = structure(list(ID = 1:3, GRIDCODE = c(1L, 1L, 1L), Shape_Leng = c(3349.48347556,
1618.93904903, 893.268790786), Shape_Area = c(309430.38861, 90015.8325676,
47507.0325775), Count = c(1L, 1L, 1L)), .Names = c("ID", "GRIDCODE",
"Shape_Leng", "Shape_Area", "Count"), row.names = 0:2, class = "data.frame")
    , polygons = list(<S4 object of class structure("Polygons", package = "sp")>,
    <S4 object of class structure("Polygons", package = "sp")>,
    <S4 object of class structure("Polygons", package = "sp")>)
    , plotOrder = 1:3
    , bbox = structure(c(476685.625393809, 311791.86152084, 508519.585393809,
312935.41622084), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"
), c("min", "max")))
    , proj4string = new("CRS"
    , projargs = "+proj=aea +lat_1=-5 +lat_2=-42 +lat_0=-32 +lon_0=-60 +x_0=0 +y_0=0 +ellps=aust_SA +units=m +no_defs"
)
)
> dput(a_10)
new("SpatialPolygonsDataFrame"
    , data = structure(list(ID = 1:5, GRIDCODE = c(1L, 1L, 1L, 1L, 1L), Shape_Leng = c(1691.7247095,
2305.45647624, 1022.64650591, 1172.27848042, 94.2722341164),
    Shape_Area = c(6.47354525991, 92111.8528756, 65.7173995386,
    19042.7776647, 415.253663691), Count = c(1L, 1L, 1L, 1L,
    1L)), .Names = c("ID", "GRIDCODE", "Shape_Leng", "Shape_Area",
"Count"), row.names = 0:4, class = "data.frame")
    , polygons = list(<S4 object of class structure("Polygons", package = "sp")>,
    <S4 object of class structure("Polygons", package = "sp")>,
    <S4 object of class structure("Polygons", package = "sp")>,
    <S4 object of class structure("Polygons", package = "sp")>,
    <S4 object of class structure("Polygons", package = "sp")>)
    , plotOrder = c(2L, 4L, 5L, 3L, 1L)
    , bbox = structure(c(825796.904693809, 815666.86152084, 831270.106493809,
816562.46752084), .Dim = c(2L, 2L), .Dimnames = list(c("x", "y"
), c("min", "max")))
    , proj4string = new("CRS"
    , projargs = "+proj=aea +lat_1=-5 +lat_2=-42 +lat_0=-32 +lon_0=-60 +x_0=0 +y_0=0 +ellps=aust_SA +units=m +no_defs"
)
)


Here is the code that I have been using:

###Load packages
library(rgdal)
library(gdistance)

###Read forest shape files
setwd("/Users/sisolarrosa/Documents/PhD/R_work/AF/IIC/R_Quest/")
shps<- dir(getwd(), "*.shp")
shps <- gsub('.{4}$', '', shps)
for (shp in shps) assign(shp, readOGR(".",layer=shp))

###Create list of spdf objects
fnps<- mget(ls(pattern= "a_"))

###For each spatial layer (object in the list), calculate distance between all polygons within layer
for (fnp in fnps)
{
  distance.matrix<- gDistance(fnp, spgeom2= NULL, byid=T);
  row.names(distance.matrix) <- paste(1:nrow(distance.matrix), sep="”);        # did this because gDistance changed the IDs of the features from [1 to ...] to [0 to ...], not sure why
  colnames(distance.matrix)<- paste(1:ncol(distance.matrix), sep="”);            # same as above
  dists.melt <- melt(distance.matrix)[melt(upper.tri(distance.matrix))$value,];  #use only lower triangle of the distances matrix
  outfile <- file.path("/Users/sisolarrosa/Documents/PhD/R_work/AF/IIC/conefor_inputs/", paste0("distances_", fnp, ".txt"));
  write.table(dists.melt, outfile,row.names=FALSE, col.names=FALSE)
}

And this is the error message:

Error in as.character.default(<S4 object of class "SpatialPolygonsDataFrame">) :
  no method for coercing this S4 class to a vector

Thank you very much!!
Cecilia


	[[alternative HTML version deleted]]



More information about the R-help mailing list