Closed bart1 closed 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
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
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!
I encountered this case where
[<-
fails. When using a data frame a warning is produced. The problem does not occur with the$
method.Created on 2024-06-06 with reprex v2.1.0