转换一列日期时间文本时出现 as.POSIXlt 错误

发布于 2025-01-10 00:31:23 字数 696 浏览 1 评论 0原文

我有一列日期时间格式的文本字段,我想将其转换为日期时间对象,以便我可以对它们进行合理的排序。我正在使用data.table

我的专栏看起来有点像这样:

Test Date
2022-01-05 04:47:05.763
NA
2022-02-16 09:51:00.000
NA
NA
2022-02-06 10:56:00.000

我将此 data.table dt 转换为日期时间对象的代码如下(旨在通过将 NA 值转换为默认日期时间来处理它们):

dt[, `Test Date` := as.POSIXlt(ifelse(is.na(`Test Date`), '2022-01-01 00:00:00', `Test Date`))]

我的错误行是:

Error in as.POSIXlt.character(ifelse(is.na(`Test Date`), "2021-01-01 00:00:00",  : 
  character string is not in a standard unambiguous format

不确定哪里出错了(不确定哪些行导致了错误)。

平台 x86_64-w64-mingw32,使用 RStudio 的 R 4.1.2

I've got a column of text fields that are in a datetime format, which I'd like to convert to datetime objects so I can sort them sanely. I'm using data.table.

My column looks a little something like this:

Test Date
2022-01-05 04:47:05.763
NA
2022-02-16 09:51:00.000
NA
NA
2022-02-06 10:56:00.000

My code to convert this data.table dt to datetime objects is as follows (meant to handle NA values by converting them to a default datetime):

dt[, `Test Date` := as.POSIXlt(ifelse(is.na(`Test Date`), '2022-01-01 00:00:00', `Test Date`))]

My error on that line is:

Error in as.POSIXlt.character(ifelse(is.na(`Test Date`), "2021-01-01 00:00:00",  : 
  character string is not in a standard unambiguous format

Unsure where I'm going wrong (unsure which rows are causing the error).

Platform x86_64-w64-mingw32, R 4.1.2 using RStudio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文