[R] Writing output of a looped process with pdfs

MacQueen, Don macqueen1 at llnl.gov
Tue Jul 28 17:18:02 CEST 2015


Having done this:

setwd("/Users/sisolarrosa/Documents/PhD/R_work/AF/IIC/split_fnp/")
shps<- dir(getwd(), "*.shp")
shps <- gsub('.{4}$', '', shps)

You can create the list directly, instead of manually, like this (not
tested, and see ?list):

  fnps <- vector('list', length(shps))
  names(fnps) <- shps

  for (shp in shps) fnps[[shp]] <- readOGR(".",layer=shp)

It is not clear from your posting exactly which command in your calculate
distances loop is failing. Have you stepped through them one at a time
manually?

Your calculation of the name outfile is probably failing because fnp is
not a character string. fnp is a spdf, so it makes no sense to use it in
   paste0("distances_", fnp, ".txt")

If you create fnps as I suggest above, then you can modify your loop along
these lines:

for (nm in names(fnps)) {
  fnp <- fnps[[nm]]
...
...  paste0("distances_", nm, ".txt")
...
}
 
Note that then names of the list fnps were calculated from a directory
listing created using dir(), so they are based on the file names. If any
of the file names have space characters in them, you will want to replace
those spaces with dots or underscores. I also don't know if the names
include the .shp suffix; if so you might want to get rid of that in the
outfile names.

In other words, I've outlined an approach, but there are likely some pesky
details in handling the list element names and output file names based on
the input file names.

-Don


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 7/25/15, 2:54 AM, "R-help on behalf of Larrosa, Cecilia"
<r-help-bounces at r-project.org on behalf of
cecilia.larrosa10 at imperial.ac.uk> wrote:

>Hi,
>
>I have created a list of spdfs, and I am looping a process for each of
>them. The process is using dDistance to calculate distances between
>features within each spdf. I want to write the output tables using the
>name of the spdf at each loop, but cannot find a way to do this. It seems
>a rather basic thing to do, and I am not very proficient in R, but I have
>spent several hours looking for a way to do this and failed. I am using R
>studio on a Mac.
>
>Here is the code so far (not the most efficient code):
>
># Load libraries
>library(rgdal)
>library(gdistance)
>
>#Read forest shape files
>setwd("/Users/sisolarrosa/Documents/PhD/R_work/AF/IIC/split_fnp/")
>shps<- dir(getwd(), "*.shp")
>shps <- gsub('.{4}$', '', shps)
>for (shp in shps) assign(shp, readOGR(".",layer=shp))
>
>#Create list (I did this manually because I could not find another way)
>fnps <- list(a_1, a_10, a_100, a_101, a_102, a_103, a_104, a_105, a_106,
>a_107, a_108, a_109, a_11, a_110,
>             a_111, a_112, a_113, a_12, a_13, a_14, a_15, a_16, a_17,
>a_18, a_19, a_2, a_20, a_21, a_22, a_23,
>             a_24, a_25,  a_26, a_27,  a_28, a_29, a_3, a_30, a_31, a_32,
>a_33, a_34, a_35, a_36, a_37,
>             a_38, a_39, a_4, a_42, a_43, a_44, a_45, a_46, a_47, a_48,
>a_49, a_5, a_50, a_51, a_52,
>             a_53, a_54, a_55,  a_56, a_57, a_6,  a_69, a_7, a_70,  a_73,
>a_79, a_8,  a_80, a_81, a_82,
>             a_83,  a_84, a_85, a_86, a_87, a_88, a_89, a_9,  a_90, a_91,
>a_94, a_95, a_96, a_98, a_99)
>
>
>###  Calculate distance between all polygons
>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")); # this is the bit that is not
>working
>  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
>
>Can anyone help me with solving the issue? How to call the name of the
>looped spdf to be included in the title of the output table? I really
>appreciate your time!
>
>Cheers
>Cecilia
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list