[R-sig-Geo] Problems with rbind(list(), makeUniqueIDs=T)

Bacou, Melanie mel at mbacou.com
Sat Jul 23 13:10:35 CEST 2016


Edzer, Rolf,
Many thanks for the clarification!
Just to confirm that Rolf's `do.call()` example works for me using the 
latest GitHub commit.

 > m <- lapply(c("TZA", "ETH", "GHA"), function(x) getData("GADM", 
country=x, level=1))
# with sp_1.2-3
 > m <- do.call(rbind, c(m, list(makeUniqueIDs=TRUE)))
Error in SpatialPolygonsDataFrame(pl, df) :
   row.names of data and Polygons IDs do not match
# with sp_1.2-4
 > m <- do.call(rbind, c(m, list(makeUniqueIDs=TRUE)))
 > class(m)
[1] "SpatialPolygonsDataFrame"

--Mel.


On 7/23/2016 5:28 AM, Edzer Pebesma wrote:
>
> On 23/07/16 05:00, Bacou, Melanie wrote:
>> Thanks! I wonder why the original syntax passing a list used to work (I
>> believe).
> It shouldn't have worked:
>
>> rbind(list(1,2,3))
>       [,1] [,2] [,3]
> [1,] 1    2    3
>> rbind(1,2,3)
>       [,1]
> [1,]    1
> [2,]    2
> [3,]    3
>
>> For a much longer (and unknown) list of SpatialPolygonsDataFrames could
>> an approach using do.call() work instead? I tried but:
>>
>>   > m <- do.call(rbind, m, makeUniqueIDs=T)
>> Error in do.call(rbind, m, makeUniqueIDs = T) :
>>     unused argument (makeUniqueIDs = T)
> See Rolf's email for the correct way of using do.call; with sp from CRAN
> it still doesn't work. As an alternative to installing sp from github
> directly, you could source the relevant rbind methods from github in
> your script:
>
> rbind.SpatialPolygons = function(..., makeUniqueIDs = FALSE) {
> 	dots = list(...)
> 	names(dots) <- NULL
> 	stopifnot(identicalCRS(dots))
> 	# checkIDSclash(dots)
> 	pl = do.call(c, lapply(dots, function(x) slot(x, "polygons")))
> 	if (makeUniqueIDs)
> 		pl = makeUniqueIDs(pl)
> 	SpatialPolygons(pl, proj4string = CRS(proj4string(dots[[1]])))
> }
> rbind.SpatialPolygonsDataFrame <- function(..., makeUniqueIDs = TRUE) {
> 	dots = list(...)
> 	names(dots) <- NULL # bugfix Clement Calenge 100417
> 	lst = lapply(dots, function(x) as(x, "SpatialPolygons"))
> 	lst$makeUniqueIDs = makeUniqueIDs
> 	pl = do.call(rbind.SpatialPolygons, lst)
> 	df = do.call(rbind, lapply(dots, function(x) x at data))
> 	SpatialPolygonsDataFrame(pl, df, match.ID = FALSE)
> }
>
>> # This works though but takes an extra step
>>   > spChFIDs(m[[1]]) <- paste0("A", seq(length(m[[1]])))
>>   > spChFIDs(m[[2]]) <- paste0("B", seq(length(m[[2]])))
>>   > spChFIDs(m[[3]]) <- paste0("C", seq(length(m[[3]])))
>>   > m <- do.call(rbind, m)
>>   > class(m)
>> [1] "SpatialPolygonsDataFrame"
>> attr(,"package")
>> [1] "sp"
>>
>> --Mel.
>>
>> On 7/22/2016 5:53 PM, Edzer Pebesma wrote:
>>> The correct call to rbind would be
>>>
>>> mm = rbind(m[[1]], m[[2]], m[[3]], makeUniqueIDs=T)
>>>
>>> with sp on CRAN this doesn't work; with the version on github it does.
>>>
>>> An alternative with sp from CRAN is to make the IDs unique by hand:
>>>
>>> spChFIDs(m[[1]]) <- paste0("A", seq(length(m[[1]])))
>>> spChFIDs(m[[2]]) <- paste0("B", seq(length(m[[2]])))
>>> spChFIDs(m[[3]]) <- paste0("C", seq(length(m[[3]])))
>>> mm = rbind(m[[1]], m[[2]], m[[3]])
>>>
>>>
>>> On 22/07/16 22:55, Bacou, Melanie wrote:
>>>> Hi,
>>>> I'm getting weird results trying to rbind a list of
>>>> SpatialPolygonsDataFrames with R 3.2.1 and raster 2.5.8. I believe the
>>>> code below used to merge all 3 country boundaries, but instead I now get
>>>> a list with 6 elements (incl. 3 logical TRUE). Am I doing something wrong?
>>>>
>>>> Thx, --Mel.
>>>>
>>>>> library(raster)
>>>>> m <- lapply(c("TZA", "UGA", "GHA"), function(x) getData("GADM",
>>>> country=x, level=1))
>>>>> m <- rbind(m, makeUniqueIDs=T)
>>>>> sapply(m, class)
>>>> [1] "SpatialPolygonsDataFrame" "logical" "SpatialPolygonsDataFrame"
>>>> [4] "logical"                  "SpatialPolygonsDataFrame" "logical"
>>>>
>>>>> sessionInfo()
>>>> R version 3.2.1 (2015-06-18)
>>>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>>>> Running under: Windows 7 x64 (build 7601) Service Pack 1
>>>>
>>>> locale:
>>>> [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United
>>>> States.1252
>>>> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>>>> [5] LC_TIME=English_United States.1252
>>>>
>>>> attached base packages:
>>>> [1] stats     graphics  grDevices utils     datasets  methods base
>>>>
>>>> other attached packages:
>>>> [1] raster_2.5-8 sp_1.2-3     rj_2.0.5-2
>>>>
>>>> loaded via a namespace (and not attached):
>>>> [1] rj.gd_2.0.0-1   Rcpp_0.12.6     grid_3.2.1 lattice_0.20-33
>>>>
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> 	[[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list