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@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.