sf
sf copied to clipboard
Error when selecting the same column twice with different names
Describe the bug A clear and concise description of what the bug is.
To Reproduce
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1
nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source
#> `/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/sf/shape/nc.shp'
#> using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS: NAD27
# basic works with sf:::select.sf
select(nc, NAME, AREA)
#> Simple feature collection with 100 features and 2 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS: NAD27
#> First 10 features:
#> NAME AREA geometry
#> 1 Ashe 0.114 MULTIPOLYGON (((-81.47276 3...
#> 2 Alleghany 0.061 MULTIPOLYGON (((-81.23989 3...
#> 3 Surry 0.143 MULTIPOLYGON (((-80.45634 3...
#> 4 Currituck 0.070 MULTIPOLYGON (((-76.00897 3...
#> 5 Northampton 0.153 MULTIPOLYGON (((-77.21767 3...
#> 6 Hertford 0.097 MULTIPOLYGON (((-76.74506 3...
#> 7 Camden 0.062 MULTIPOLYGON (((-76.00897 3...
#> 8 Gates 0.091 MULTIPOLYGON (((-76.56251 3...
#> 9 Warren 0.118 MULTIPOLYGON (((-78.30876 3...
#> 10 Stokes 0.124 MULTIPOLYGON (((-80.02567 3...
# but trying to select the same column twice
# *with different names* doesn't work
select(nc, name1 = NAME, name2 = NAME, AREA)
#> Error in names(object) <- nm: 'names' attribute [3] must be the same length as the vector [2]
# Error in names(object) <- nm :
# 'names' attribute [3] must be the same length as the vector [2]
# this _is_ okay with dplyr::select.data.frame, though
select(starwars, name1 = name, name2 = name, height)
#> # A tibble: 87 × 3
#> name1 name2 height
#> <chr> <chr> <int>
#> 1 Luke Skywalker Luke Skywalker 172
#> 2 C-3PO C-3PO 167
#> 3 R2-D2 R2-D2 96
#> 4 Darth Vader Darth Vader 202
#> 5 Leia Organa Leia Organa 150
#> 6 Owen Lars Owen Lars 178
#> 7 Beru Whitesun lars Beru Whitesun lars 165
#> 8 R5-D4 R5-D4 97
#> 9 Biggs Darklighter Biggs Darklighter 183
#> 10 Obi-Wan Kenobi Obi-Wan Kenobi 182
#> # … with 77 more rows
Created on 2022-01-14 by the reprex package (v2.0.1)
Additional context
Since this is allowed with dplyr, users might either expect that they can do it with sf, or they might accidentally select the same column twice and not realise what the problem is. A more informative error message might help users. For example:
Error: source column
NAMEwas selected more than once asname1andname2.
Matrix products: default BLAS: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRblas.0.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib
locale: [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] reprex_2.0.1 sf_1.0-3 dplyr_1.0.7
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 highr_0.9 pillar_1.6.4 compiler_4.1.2
[5] R.utils_2.11.0 R.methodsS3_1.8.1 class_7.3-19 tools_4.1.2
[9] digest_0.6.29 evaluate_0.14 jsonlite_1.7.2 lifecycle_1.0.1
[13] tibble_3.1.6 R.cache_0.15.0 pkgconfig_2.0.3 rlang_0.4.12
[17] rstudioapi_0.13 DBI_1.1.1 cli_3.1.0 yaml_2.2.1
[21] xfun_0.29 fastmap_1.1.0 e1071_1.7-9 knitr_1.36
[25] withr_2.4.3 styler_1.6.2 generics_0.1.1 vctrs_0.3.8
[29] fs_1.5.0 classInt_0.4-3 grid_4.1.2 tidyselect_1.1.1
[33] glue_1.5.1 R6_2.5.1 processx_3.5.2 fansi_0.5.0
[37] rmarkdown_2.11 callr_3.7.0 clipr_0.7.1 purrr_0.3.4
[41] magrittr_2.0.1 ps_1.6.0 htmltools_0.5.2 backports_1.3.0
[45] ellipsis_0.3.2 units_0.7-2 assertthat_0.2.1 utf8_1.2.2
[49] KernSmooth_2.23-20 proxy_0.4-26 crayon_1.4.2 R.oo_1.24.0
sf::sf_extSoftVersion() GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H "3.9.1" "3.2.3" "7.2.1" "true" "true" PROJ "7.2.1"
</details>