SisoL
2015-Jul-27 16:33 UTC
[R] Varying name of output tables from looped process of list of spdf objects
Hi Peter,
Thank you for your reply. The method for looping seems to work, but
gDistance will not recognise the input. I am puzzled because when I
print(fnp), and print (a_10) they look exactly the same, but when I try to
run gDistance{rgeos} with a_10 it works, but with fnp it throws an error.
Please see below. Any other suggestions?
>afiles <- ls(pattern= "a_") #OK
>print(afiles)
[1] "a_1" "a_10"
>fnp<- mget(afiles[ifile]) #OK
> print(fnp)
$a_10
class : SpatialPolygonsDataFrame
features : 5
extent : 825796.9, 831270.1, 815666.9, 816562.5 (xmin, xmax, ymin,
ymax)
coord. ref. : +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
variables : 5
names : ID, GRIDCODE, Shape_Leng, Shape_Area, Count
min values : 1, 1, 94.2722341164, 6.47354525991, 1
max values : 5, 1, 2305.45647624, 92111.8528756, 1
> print(a_10)
class : SpatialPolygonsDataFrame
features : 5
extent : 825796.9, 831270.1, 815666.9, 816562.5 (xmin, xmax, ymin,
ymax)
coord. ref. : +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
variables : 5
names : ID, GRIDCODE, Shape_Leng, Shape_Area, Count
min values : 1, 1, 94.2722341164, 6.47354525991, 1
max values : 5, 1, 2305.45647624, 92111.8528756, 1
>distance.matrix<- gDistance(fnp, spgeom2= NULL, byid=T)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ?is.projected? for
signature ?"list"?
--
View this message in context:
http://r.789695.n4.nabble.com/Varying-name-of-output-tables-from-looped-process-of-list-of-spdf-objects-tp4710369p4710408.html
Sent from the R help mailing list archive at Nabble.com.
Anthoni, Peter (IMK)
2015-Jul-27 17:51 UTC
[R] Varying name of output tables from looped process of list of spdf objects
Hi Cecilia, print(fnp) and print(a_10) are not exactly the same>> print(fnp) > $a_10 > class : SpatialPolygonsDataFramevs.>> print(a_10) > class : SpatialPolygonsDataFrameLooks like mget returns a list. Can you try: fnp <- get(afiles[ifile])[1] print(fnp) #the $a_10 should be gone. cheers Peter> On 27 Jul 2015, at 18:33, SisoL <cecilia.larrosa10 at imperial.ac.uk> wrote: > > Hi Peter, > > Thank you for your reply. The method for looping seems to work, but > gDistance will not recognise the input. I am puzzled because when I > print(fnp), and print (a_10) they look exactly the same, but when I try to > run gDistance{rgeos} with a_10 it works, but with fnp it throws an error. > Please see below. Any other suggestions? > >> afiles <- ls(pattern= "a_") #OK >> print(afiles) > [1] "a_1" "a_10" > >> fnp<- mget(afiles[ifile]) #OK > >> print(fnp) > $a_10 > class : SpatialPolygonsDataFrame > features : 5 > extent : 825796.9, 831270.1, 815666.9, 816562.5 (xmin, xmax, ymin, > ymax) > coord. ref. : +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 > variables : 5 > names : ID, GRIDCODE, Shape_Leng, Shape_Area, Count > min values : 1, 1, 94.2722341164, 6.47354525991, 1 > max values : 5, 1, 2305.45647624, 92111.8528756, 1 > >> print(a_10) > class : SpatialPolygonsDataFrame > features : 5 > extent : 825796.9, 831270.1, 815666.9, 816562.5 (xmin, xmax, ymin, > ymax) > coord. ref. : +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 > variables : 5 > names : ID, GRIDCODE, Shape_Leng, Shape_Area, Count > min values : 1, 1, 94.2722341164, 6.47354525991, 1 > max values : 5, 1, 2305.45647624, 92111.8528756, 1 > >> distance.matrix<- gDistance(fnp, spgeom2= NULL, byid=T) > Error in (function (classes, fdef, mtable) : > unable to find an inherited method for function ?is.projected? for > signature ?"list"? > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Varying-name-of-output-tables-from-looped-process-of-list-of-spdf-objects-tp4710369p4710408.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Sarah Goslee
2015-Jul-27 18:11 UTC
[R] Varying name of output tables from looped process of list of spdf objects
Ah, but they don't look exactly the same. Look closer at the first line of the print() output. You might also look at class(fnp) class(a_10) for a clue. What happens if you run: distance.matrix<- gDistance(fnp[[1]], spgeom2= NULL, byid=T) Sarah On Mon, Jul 27, 2015 at 12:33 PM, SisoL <cecilia.larrosa10 at imperial.ac.uk> wrote:> Hi Peter, > > Thank you for your reply. The method for looping seems to work, but > gDistance will not recognise the input. I am puzzled because when I > print(fnp), and print (a_10) they look exactly the same, but when I try to > run gDistance{rgeos} with a_10 it works, but with fnp it throws an error. > Please see below. Any other suggestions? > >>afiles <- ls(pattern= "a_") #OK >>print(afiles) > [1] "a_1" "a_10" > >>fnp<- mget(afiles[ifile]) #OK > >> print(fnp) > $a_10 > class : SpatialPolygonsDataFrame > features : 5 > extent : 825796.9, 831270.1, 815666.9, 816562.5 (xmin, xmax, ymin, > ymax) > coord. ref. : +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 > variables : 5 > names : ID, GRIDCODE, Shape_Leng, Shape_Area, Count > min values : 1, 1, 94.2722341164, 6.47354525991, 1 > max values : 5, 1, 2305.45647624, 92111.8528756, 1 > >> print(a_10) > class : SpatialPolygonsDataFrame > features : 5 > extent : 825796.9, 831270.1, 815666.9, 816562.5 (xmin, xmax, ymin, > ymax) > coord. ref. : +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 > variables : 5 > names : ID, GRIDCODE, Shape_Leng, Shape_Area, Count > min values : 1, 1, 94.2722341164, 6.47354525991, 1 > max values : 5, 1, 2305.45647624, 92111.8528756, 1 > >>distance.matrix<- gDistance(fnp, spgeom2= NULL, byid=T) > Error in (function (classes, fdef, mtable) : > unable to find an inherited method for function ?is.projected? for > signature ?"list"? >-- Sarah Goslee http://www.functionaldiversity.org
SisoL
2015-Jul-28 15:36 UTC
[R] Varying name of output tables from looped process of list of spdf objects
Hi,
Thank you for the replies. I tried Peter's suggestions and this worked:
###Read forest shape files
setwd("/Users/sisolarrosa/Documents/PhD/R_work/AF/IIC/split_bf_fnp/")
shps<- dir(getwd(), "*.shp")
shps <- gsub('.{4}$', '', shps)
for (shp in shps) assign(shp, readOGR(".",layer=shp))
###Create list
afiles <- ls(pattern= "a_")
### Create Conefor Input Tables
for (ifile in 1:length(afiles))
{
fnp<- mget(afiles[ifile])[[1]]
distance.matrix<- gDistance(fnp, spgeom2= NULL, byid=T);
row.names(distance.matrix) <- paste(1:nrow(distance.matrix),
sep="");
colnames(distance.matrix)<- paste(1:ncol(distance.matrix),
sep="");
dists.melt <-
melt(distance.matrix)[melt(upper.tri(distance.matrix))$value,];
outfile <-
file.path("/Users/sisolarrosa/Documents/PhD/R_work/AF/IIC/conefor_inputs/",
paste0("distances_", afiles[ifile], ".txt"));
write.table(dists.melt, outfile,row.names=FALSE, col.names=FALSE);
}
--
View this message in context:
http://r.789695.n4.nabble.com/Varying-name-of-output-tables-from-looped-process-of-list-of-spdf-objects-tp4710369p4710491.html
Sent from the R help mailing list archive at Nabble.com.