Skip to content

Error in unclass(x) + conform(Vec, x) : non-numeric argument to binary operator #2319

@mt1022

Description

@mt1022

Describe the bug
Add operation failed when the order of items changed.

To Reproduce

library(sf)
library(spData)
nz_sfc <- st_geometry(nz)
nz_sfc_cent <- st_centroid(nz_sfc)

# works
(nz_sfc - nz_sfc_cent) * 0.5 + nz_sfc_cent

# same (only order changed), but does not work
nz_sfc_cent + (nz_sfc - nz_sfc_cent) * 0.5

output:

> nz_sfc <- st_geometry(nz)
> nz_sfc_cent <- st_centroid(nz_sfc)
> (nz_sfc - nz_sfc_cent) * 0.5 + nz_sfc_cent
Geometry set for 16 features 
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 1147370 ymin: 4836672 xmax: 2060191 ymax: 6130942
CRS:           NA
First 5 geometries:
MULTIPOLYGON (((1710099 6035907, 1707622 603253...
MULTIPOLYGON (((1778687 5917378, 1772497 591766...
MULTIPOLYGON (((1839928 5823696, 1838660 582082...
MULTIPOLYGON (((1992348 5801244, 1993162 579806...
MULTIPOLYGON (((2027669 5713317, 2024943 571452...
> nz_sfc_cent + (nz_sfc - nz_sfc_cent) * 0.5
Error in unclass(x) + conform(Vec, x) : 
  non-numeric argument to binary operator

If reporting a change from previous versions

Please read https://cran.r-project.org/web/packages/sf/news/news.html first.

Additional context
The example is from the online book here: https://r.geocompx.org/geometry-operations

Paste the output of your `sessionInfo()` and `sf::sf_extSoftVersion()`
> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.2.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Asia/Shanghai
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] spDataLarge_2.1.1 spData_2.3.0      dplyr_1.1.2       terra_1.7-55      sf_1.0-14        

loaded via a namespace (and not attached):
 [1] compiler_4.3.2     tidyselect_1.2.0   Rcpp_1.0.11        scales_1.2.1       lattice_0.21-9    
 [6] R6_2.5.1           lobstr_1.1.2       sfheaders_0.4.3    generics_0.1.3     classInt_0.4-9    
[11] s2_1.1.4           knitr_1.45         tibble_3.2.1       units_0.8-3        munsell_0.5.0     
[16] DBI_1.1.3          pillar_1.9.0       rlang_1.1.2        utf8_1.2.4         sp_2.0-0          
[21] xfun_0.41          pkgload_1.3.2.1    cli_3.6.1          magrittr_2.0.3     wk_0.8.0          
[26] class_7.3-22       grid_4.3.2         rstudioapi_0.15.0  lifecycle_1.0.4    prettyunits_1.2.0 
[31] vctrs_0.6.4        KernSmooth_2.23-22 proxy_0.4-27       glue_1.6.2         codetools_0.2-19  
[36] fansi_1.0.5        e1071_1.7-13       colorspace_2.1-0   tools_4.3.2        pkgconfig_2.0.3   
> sf::sf_extSoftVersion()
          GEOS           GDAL         proj.4 GDAL_with_GEOS     USE_PROJ_H           PROJ 
      "3.11.0"        "3.5.3"        "9.1.0"         "true"         "true"        "9.1.0"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions