[R-sig-Geo] Merging a list of SpatialPointsDataFrame objects

Michael Sumner mdsumner at gmail.com
Thu Aug 11 05:51:25 CEST 2011


This is what do.call is for:

 do.call("rbind", meuse_list)




On Thu, Aug 11, 2011 at 1:39 PM, Jonathan Greenberg
<greenberg at ucdavis.edu> wrote:
> I used a mapply() statement to create a list of SpatialPointsDataFrame
> objects -- each of the list entries is a SpatialPointsDataFrame
> object, and each SpatialPointsDataFrame object has the identical @data
> *names* (e.g. the dataframe is rbindable for all of the objects).
> What I'm wondering is if there is an efficient way to merge all of
> these into a single SpatialPointsDataFrame object -- I can (I suppose)
> do an rbind or spRbind, but it seems like I'd have to loop through the
> list entry one at a time, appending each successive
> SpatialPointsDataFrame object into the growing "mega"
> SpatialPointsDataFrame.  Is there a cleaner/faster way to do this in
> R?  Thanks!
>
> Here's an example using the built in data/examples:
>
> require(sp)
> data(meuse) # reload data.frame
> coordinates(meuse) = c("x", "y") # specify column names
>
> meuse1=meuse[1:10,]
> meuse2=meuse[11:20,]
> meuse3=meuse[21:50,]
> meuse_list=list(meuse1,meuse2,meuse3)
>
> # Brute Force way:
> for(i in 1:length(meuse_list))
> {
>        if(i==1)
>        {
>                meuse_merged=meuse_list[[i]]
>        } else
>        {
>                meuse_merged=rbind(meuse_merged,meuse_list[[i]])
>        }
> }
>
>
>
> --j
>
> --
> Jonathan A. Greenberg, PhD
> Assistant Project Scientist
> Center for Spatial Technologies and Remote Sensing (CSTARS)
> Department of Land, Air and Water Resources
> University of California, Davis
> One Shields Avenue
> Davis, CA 95616
> Phone: 415-763-5476
> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list