Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mutate(.keep = "none") and mutate(.keep = "used") error with sf object #2326

Open
florisvdh opened this issue Jan 31, 2024 · 1 comment
Open

Comments

@florisvdh
Copy link
Member

florisvdh commented Jan 31, 2024

Describe the bug

1. transmute(<sf-object>, ...) vs. mutate(<sf-object>, ..., .keep = "none")

The documentation of transmute() says:

It's superseded because you can perform the same job with mutate(.keep = "none").

However mutate(<sf-object>, ..., .keep = "none") errors, while it is expected to give the same result as transmute().

Notes:

  • The geometry column should be sticky in mutate(), as in transmute().
  • bind_cols() seems to work around the error, but looses the geometry; this may hint at what's happening.

2. mutate(<sf-object>, ..., .keep = "used")

mutate(<sf-object>, ..., .keep = "used") returns the same error.

I'd think in both cases the sticky nature of the geometry column is not preserved / respected, leading to the error.

To Reproduce

library(dplyr, warn.conflicts = FALSE)
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.6.4, PROJ 9.1.1; sf_use_s2() is TRUE
sf_extSoftVersion()
#>           GEOS           GDAL         proj.4 GDAL_with_GEOS     USE_PROJ_H 
#>       "3.11.1"        "3.6.4"        "9.1.1"         "true"         "true" 
#>           PROJ 
#>        "9.1.1"

g <- st_sfc(st_point(1:2), st_point(3:4))
s <- st_sf(a = 3:4, b = 1:2, geometry = g)
s
#> Simple feature collection with 2 features and 2 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> CRS:           NA
#>   a b    geometry
#> 1 3 1 POINT (1 2)
#> 2 4 2 POINT (3 4)

# both should be the same: ------------------------------------
transmute(s, c = a * 2)
#> Simple feature collection with 2 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> CRS:           NA
#>   c    geometry
#> 1 6 POINT (1 2)
#> 2 8 POINT (3 4)
mutate(s, c = a * 2, .keep = "none")
#> Error in st_geometry.sf(x): attr(obj, "sf_column") does not point to a geometry column.
#> Did you rename it, without setting st_geometry(obj) <- "newname"?

# strangely the following works, BUT drops geometry: ----------
mutate(s, c = a * 2, .keep = "none") |>
  bind_cols()
#>   c
#> 1 6
#> 2 8

# also errors: ------------------------------------------------
mutate(s, c = a * 2, .keep = "used")
#> Error in st_geometry.sf(x): attr(obj, "sf_column") does not point to a geometry column.
#> Did you rename it, without setting st_geometry(obj) <- "newname"?

# these are no problem: ---------------------------------------
mutate(s, c = a * 2, .keep = "unused")
#> Simple feature collection with 2 features and 2 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> CRS:           NA
#>   b    geometry c
#> 1 1 POINT (1 2) 6
#> 2 2 POINT (3 4) 8
mutate(s, c = a * 2, .keep = "all")
#> Simple feature collection with 2 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1 ymin: 2 xmax: 3 ymax: 4
#> CRS:           NA
#>   a b    geometry c
#> 1 3 1 POINT (1 2) 6
#> 2 4 2 POINT (3 4) 8

Created on 2024-01-31 with reprex v2.1.0

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.2 (2023-10-31)
#>  os       Linux Mint 21.3
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language nl_BE:nl
#>  collate  nl_BE.UTF-8
#>  ctype    nl_BE.UTF-8
#>  tz       Europe/Brussels
#>  date     2024-01-31
#>  pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  class         7.3-22  2023-05-03 [3] RSPM (R 4.2.0)
#>  classInt      0.4-10  2023-09-05 [3] RSPM (R 4.3.0)
#>  cli           3.6.2   2023-12-11 [3] RSPM (R 4.3.0)
#>  DBI           1.2.1   2024-01-12 [3] RSPM (R 4.3.0)
#>  digest        0.6.34  2024-01-11 [3] RSPM (R 4.3.0)
#>  dplyr       * 1.1.4   2023-11-17 [3] RSPM (R 4.3.0)
#>  e1071         1.7-14  2023-12-06 [3] RSPM (R 4.3.0)
#>  evaluate      0.23    2023-11-01 [3] RSPM (R 4.3.0)
#>  fansi         1.0.6   2023-12-08 [3] RSPM (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [3] RSPM (R 4.2.0)
#>  fs            1.6.3   2023-07-20 [3] RSPM (R 4.2.0)
#>  generics      0.1.3   2022-07-05 [3] RSPM (R 4.2.0)
#>  glue          1.7.0   2024-01-09 [3] RSPM (R 4.3.0)
#>  htmltools     0.5.7   2023-11-03 [3] RSPM (R 4.3.0)
#>  KernSmooth    2.23-22 2023-07-10 [3] RSPM (R 4.2.0)
#>  knitr         1.45    2023-10-30 [3] RSPM (R 4.3.0)
#>  lifecycle     1.0.4   2023-11-07 [3] RSPM (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [3] RSPM (R 4.2.0)
#>  pillar        1.9.0   2023-03-22 [3] RSPM (R 4.2.0)
#>  pkgconfig     2.0.3   2019-09-22 [3] CRAN (R 4.0.1)
#>  proxy         0.4-27  2022-06-09 [3] RSPM (R 4.2.0)
#>  purrr         1.0.2   2023-08-10 [3] RSPM (R 4.2.0)
#>  R.cache       0.16.0  2022-07-21 [3] RSPM (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [3] RSPM (R 4.2.0)
#>  R.oo          1.26.0  2024-01-24 [3] RSPM (R 4.3.0)
#>  R.utils       2.12.3  2023-11-18 [3] RSPM (R 4.3.0)
#>  R6            2.5.1   2021-08-19 [3] RSPM (R 4.2.0)
#>  Rcpp          1.0.12  2024-01-09 [3] RSPM (R 4.3.0)
#>  reprex        2.1.0   2024-01-11 [3] RSPM (R 4.3.0)
#>  rlang         1.1.3   2024-01-10 [3] RSPM (R 4.3.0)
#>  rmarkdown     2.25    2023-09-18 [3] RSPM (R 4.3.0)
#>  rstudioapi    0.15.0  2023-07-07 [3] RSPM (R 4.2.0)
#>  sessioninfo   1.2.2   2021-12-06 [3] RSPM (R 4.2.0)
#>  sf          * 1.0-15  2023-12-18 [1] CRAN (R 4.3.2)
#>  styler        1.10.2  2023-08-29 [3] RSPM (R 4.2.0)
#>  tibble        3.2.1   2023-03-20 [3] RSPM (R 4.3.0)
#>  tidyselect    1.2.0   2022-10-10 [3] RSPM (R 4.2.0)
#>  units         0.8-5   2023-11-28 [3] RSPM (R 4.3.0)
#>  utf8          1.2.4   2023-10-22 [3] RSPM (R 4.3.0)
#>  vctrs         0.6.5   2023-12-01 [3] RSPM (R 4.3.0)
#>  withr         3.0.0   2024-01-16 [3] RSPM (R 4.3.2)
#>  xfun          0.41    2023-11-01 [3] RSPM (R 4.3.0)
#>  yaml          2.3.8   2023-12-11 [3] RSPM (R 4.3.0)
#> 
#>  [1] /home/floris/lib/R/library
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@edzer
Copy link
Member

edzer commented Jan 31, 2024

Thanks, FYI,

bind_cols() seems to work around the error, but looses the geometry; this may hint at what's happening.

bind_cols() is a function, not a method, so there's little we can do about that here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants