[R] Writing output of a looped process with pdfs

John Kane jrkrideau at inbox.com
Sat Jul 25 15:01:25 CEST 2015


Hi Cecilia,
I am of no help with your actual problem but here are a few suggestions to help improve your question. 
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and http://adv-r.had.co.nz/Reproducibility.html

I see you say that you created fnps list by hand. There is a much easier way to do this. You can use the dput() command to provide us with an identical copy of the data as it appears on your machine. Just do dput(fnps) and copy the output into your email.  dput() is discussed in the links above or see ?dput. Actually we probably would like to see the shps in its form as soon as you load it.  

Sorry not to have been of any substantive help.


By the way, the problem  is very well stated but a major issue, at least to me, is the "list" you supply. I may just be obtuse but I don't see how you can read it in as a list() nor do I understand why. It may be a misunderstanding of data structures in R (which are great once you get your mind wrapped around them)

I can read in fnps if I treat it as a character vector. What I have done here is use the Hmisc package's function Cs to read in a character vector without having to have quotes "" around each item. It is the equivalent of fnps  <-  c("a_1", "a_10" ... )
library(Hmisc)
fnps <- Cs(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)

John Kane
Kingston ON Canada


> -----Original Message-----
> From: cecilia.larrosa10 at imperial.ac.uk
> Sent: Sat, 25 Jul 2015 09:54:00 +0000
> To: r-help at r-project.org
> Subject: [R] Writing output of a looped process with pdfs
> 
> 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.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list