【问题标题】:as.POSIXct giving ambiguous error in r datatableas.POSIXct 在 r 数据表中给出模棱两可的错误
【发布时间】:2020-09-30 22:04:11
【问题描述】:

我有一个包含一列时间戳的数据表。 我创建了一个名为 date 的新列,使用:setDT(data_test)[,date := as.IDate(ev_date)]

data_test = data_test[,new := as.POSIXct(paste(date, "00:00:00"))]

我收到以下错误:

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

我想找出两个时间戳之间的差异,一个是基于另一个时间戳的日期...例如,如果我的时间戳是 2020-05-18 03:23:45 ,那么我正在尝试 2020-05-18 03:23:45 - 2020-05-18 00:00:00

【问题讨论】:

  • 知道date的值会有所帮助。
  • 什么意思,..????
  • as.POSIXct(paste(date, "00:00:00")):您将date00:00:00 连接起来。错误消息表明date 不是预期的格式。所以为了帮助你,我们需要知道date 有什么价值。这是基本的东西。
  • date 的值是多少? dput(data_test$date[1:10]) 返回什么?

标签: r data.table


【解决方案1】:

您当然有理由使用experimental date format

转换为 POSIXct 是可能的,因为它是一个不能使用 HH:MM:SS 的日期

df <- data.frame(ev_date = as.Date('2020-05-18 03:23:45')) %>% as.data.table()
df[,idate := as.IDate(ev_date)][,posix_date := as.POSIXct(idate)]
df
      ev_date      idate posix_date
1: 2020-06-11 2020-06-11 2020-06-11

如果您只想计算两个时间戳之间的差异,则应使用 Datetime 而不是仅使用日期。 POSIXct 是这方面的常用标准:

difftime(as.POSIXct('2020-05-18 03:23:45'),as.POSIXct(as.POSIXct('2020-05-18 00:00:00')),unit='hours')
   
Time difference of 3.395833 hours

【讨论】:

  • 我想找出两个时间戳之间的差异,一个是基于另一个时间戳的日期...例如,如果我的时间戳是 2020-05-18 03:23:45 ,那么我正在尝试 2020-05-18 03:23:45 - 2020-05-18 00:00:00
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 2016-02-13
  • 2013-06-10
  • 1970-01-01
相关资源
最近更新 更多