r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.35k stars 299 forks source link

`st_agr` fails in `[<-` when assigning a `POSIXlt` to an `sf` #2401

Closed bart1 closed 5 months ago

bart1 commented 5 months ago

I encountered this case where [<- fails. When using a data frame a warning is produced. The problem does not occur with the $ method.

require(sf)
#> Loading required package: sf
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
require(tibble)
#> Loading required package: tibble
pt1 = st_point(c(0,1))
pt2 = st_point(c(1,1))
d = tibble(a = 1:2)
d$geom = st_sfc(pt1, pt2)
df = st_as_sf(d)[rep(1:2,10),]
df$t1<-as.POSIXct(Sys.time())
df$t2<-as.POSIXlt(Sys.time())
df[,'t3']<-as.POSIXlt(Sys.time())
#> Error in `st_agr()`:
#> ! Can't recycle input of size 11 to size 1.
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.0 (2024-04-24)
#>  os       Ubuntu 22.04.4 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Amsterdam
#>  date     2024-06-06
#>  pandoc   3.1.11 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/x86_64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  class         7.3-22  2023-05-03 [4] CRAN (R 4.3.1)
#>  classInt      0.4-10  2023-09-05 [1] CRAN (R 4.4.0)
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.4.0)
#>  DBI           1.2.2   2024-02-16 [1] CRAN (R 4.4.0)
#>  digest        0.6.35  2024-03-11 [1] CRAN (R 4.4.0)
#>  e1071         1.7-14  2023-12-06 [1] CRAN (R 4.4.0)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.4.0)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.4.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.4.0)
#>  fs            1.6.4   2024-04-25 [1] CRAN (R 4.4.0)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#>  KernSmooth    2.23-22 2023-07-10 [4] CRAN (R 4.3.1)
#>  knitr         1.46    2024-04-06 [1] CRAN (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.4.0)
#>  proxy         0.4-27  2022-06-09 [1] CRAN (R 4.4.0)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.4.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.4.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.4.0)
#>  R.oo          1.26.0  2024-01-24 [1] CRAN (R 4.4.0)
#>  R.utils       2.12.3  2023-11-18 [1] CRAN (R 4.4.0)
#>  Rcpp          1.0.12  2024-01-09 [1] CRAN (R 4.4.0)
#>  reprex        2.1.0   2024-01-11 [1] CRAN (R 4.4.0)
#>  rlang         1.1.3   2024-01-10 [1] CRAN (R 4.4.0)
#>  rmarkdown     2.26    2024-03-05 [1] CRAN (R 4.4.0)
#>  rstudioapi    0.16.0  2024-03-24 [1] CRAN (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
#>  sf          * 1.0-16  2024-03-24 [1] CRAN (R 4.4.0)
#>  styler        1.10.3  2024-04-07 [1] CRAN (R 4.4.0)
#>  tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.4.0)
#>  units         0.8-5   2023-11-28 [1] CRAN (R 4.4.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.4.0)
#>  xfun          0.43    2024-03-25 [1] CRAN (R 4.4.0)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.4.0)
#> 
#>  [1] /home/bart/R/x86_64-pc-linux-gnu-library/4.4
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Created on 2024-06-06 with reprex v2.1.0

edzer commented 5 months ago

Because as.POSIXlt() returns a length 11 list:

require(tibble)
# Loading required package: tibble
d = tibble(a = 1:2)
d[,'t3']<-as.POSIXlt(Sys.time())
# Error in `[<-`:
# ! Can't recycle input of size 11 to size 1.
# Backtrace:
#     ▆
#  1. ├─base::`[<-`(`*tmp*`, , "t3", value = `<dttm>`)
#  2. ├─tibble:::`[<-.tbl_df`(`*tmp*`, , "t3", value = `<dttm>`)
#  3. │ └─tibble:::tbl_subassign(x, i, j, value, i_arg, j_arg, substitute(value))
#  4. │   └─tibble:::vectbl_recycle_rhs_cols(value, length(j), call)
#  5. │     └─vctrs::vec_recycle(value, ncol, call = call)
#  6. └─vctrs:::stop_recycle_incompatible_size(...)
#  7.   └─vctrs:::stop_vctrs(...)
#  8.     └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
# Execution halted
edzer commented 5 months ago

You may have meant to use as.POSIXct, or else

require(tibble)
# Loading required package: tibble
d = tibble(a = 1:2)
d[,'t3']<-list(as.POSIXlt(Sys.time()))
d
# # A tibble: 2 × 2
#       a t3                 
#   <int> <dttm>             
# 1     1 2024-06-06 12:39:26
# 2     2 2024-06-06 12:39:26
bart1 commented 5 months ago

In most cases as.POSIXct would be a good alternative, this one was encountered as rounding (round(..., units="secs")) a ct returns a lt. I did not know about the list solution, thanks!