rbindlist messing up with bouding box of spatial `sf` objects

Open
#5,352 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start with the minimal reproducible example using geobr, sf, and data.table, then compare rbindlist(test_list, fill = TRUE) with base rbind(rr, rs). Trace how the spatial bounding-box attribute changes and use the waldo comparisons to verify that the resulting sf object preserves the expected bbox.

Written by the indexing model from the issue text.

Description

[rbind|cbind|merge]list non-atomic column

I've found that rbindlist{data.table} somehow changes the bouding box of spatial sf objects. This is related to issue #2273 here, and I've linked to issues on geobr and tmap packages as well.

Minimal reproducible example

devtools::install_github("ipeaGIT/geobr", subdir = "r-package")
library(geobr)
library(sf)
library(data.table)
library(waldo)

# download sf data
rr <- read_state(code_state = 'RR')
rs <- read_state(code_state = 'RS')

test_list <- list(rr, rs)

# row bind with rbindlist
t1_list <- data.table::rbindlist(test_list, fill = TRUE)
t1 <- sf::st_sf(t1_list) 
plot(t1['code_state'])

# base row bind
t2 <- rbind(rr,rs)
plot(t2['code_state'])

# compare
waldo::compare(t1, t2)

> `class(old)`: "sf" "data.table" "data.frame"
> `class(new)`: "sf"              "data.frame"
> 
> `attr(old$geom, 'bbox')`: "((-64.82525,-1.580633),(-58.88688,5.271841))"
> `attr(new$geom, 'bbox')`: "((-64.82525,-33.75208),(-49.69146,5.271841))"

For some reason, though, this problem is fixed when I run a simple subset removing a row hat does not exist in the data.

t3 <- subset(t1, abbrev_state  != "xx")
waldo::compare(t2, t3)

> `class(old)`: "sf" "data.frame"             
> `class(new)`: "sf" "data.table" "data.frame"

sessionInfo()

> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] 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] waldo_0.4.0       data.table_1.14.2 sf_1.0-7          geobr_1.6.6      

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3       rstudioapi_0.13    rematch2_2.1.2    
 [4] magrittr_2.0.2     units_0.8-0        tidyselect_1.1.2  
 [7] R6_2.5.1           rlang_1.0.2        fansi_1.0.2       
[10] httr_1.4.2         dplyr_1.0.8        tools_4.1.1       
[13] grid_4.1.1         utf8_1.2.2         KernSmooth_2.23-20
[16] cli_3.1.1          e1071_1.7-9        DBI_1.1.2         
[19] ellipsis_0.3.2     class_7.3-19       assertthat_0.2.1  
[22] tibble_3.1.6       lifecycle_1.0.1    crayon_1.5.0      
[25] purrr_0.3.4        vctrs_0.3.8        curl_4.3.2        
[28] glue_1.6.2         proxy_0.4-26       diffobj_0.3.5     
[31] compiler_4.1.1     pillar_1.7.0       generics_0.1.2    
[34] classInt_0.4-3     pkgconfig_2.0.3   

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.