Iterate in a Spatial Polygon Dataframe in R?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Iterate in a Spatial Polygon Dataframe in R?

R help mailing list-2

Hi all,

 I'm trying to iterate in a SpatialPolygonDataFrame thatcontains 110 features. I tried to use the following code 
iterate.spdf =function(x){ for (i in 1:nrow(x)){ p = x[i, ]} return(p)}
but with no success as it returns with aSpatialPolygonDataFrame that contains onlyone feature. The code seems to overwrite the polygons to end up with only onefeature.
 Any suggestions?Ashraf, cheers


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Iterate in a Spatial Polygon Dataframe in R?

Jon.SKOIEN

Ashraf,

What do you want your function to do?
Currently you are overwriting p each time you go through your for-loop, and you finally return p = x[nrow(x), ]

It is similar to

y = 101:200
for (i in 1:100) {
  x = y[i]
}
x


Best,
Jon


--
Jon Olav Skøien
European Commission
Joint Research Centre – JRC.E.1
Disaster Risk Management Unit
Building 26b 1/144 | Via E.Fermi 2749, I-21027 Ispra (VA) Italy, TP 267
Disclaimer: Views expressed in this email are those of the individual and do not necessarily represent official views of the European Commission.


________________________________________
From: R-help [[hidden email]] on behalf of Ashraf Afana via R-help [[hidden email]]
Sent: 11 October 2017 13:55
To: [hidden email]
Subject: [R] Iterate in a Spatial Polygon Dataframe in R?

Hi all,

 I'm trying to iterate in a SpatialPolygonDataFrame thatcontains 110 features. I tried to use the following code
iterate.spdf =function(x){ for (i in 1:nrow(x)){ p = x[i, ]} return(p)}
but with no success as it returns with aSpatialPolygonDataFrame that contains onlyone feature. The code seems to overwrite the polygons to end up with only onefeature.
 Any suggestions?Ashraf, cheers


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.