在 R 中将字符时间戳转换为日期时间戳; H:M:S 不断被删除

Converting character timestamp to date-time stamp in R; H:M:S keeps getting removed

我正在尝试将字符日期时间戳转换为 R 中的规范化日期时间戳,但 运行 变成了具有多种不同解决方案的同一问题。这是一个示例:-

timesdf<-structure(list(DateTime = c("2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00")), row.names = c(NA, 15L), class = "data.frame")


str(timesdf)
#'data.frame':  15 obs. of  1 variable:
#  $ DateTime: chr  "2021-02-20 00:00:00" "2021-02-20 00:00:00" "2021-02-20 00:00:00" "2021-02-20 00:00:00" ...

以下是我尝试过的一些解决方案:-

#lubridate solution 1
timesdf$DateTime<-ymd_hms(timesdf$DateTime)
timesdf
head(timesdf)
#    DateTime
#1 2021-02-20
#2 2021-02-20
#3 2021-02-20
#4 2021-02-20
#5 2021-02-20
#6 2021-02-20


#lubridate solution 2
timesdf$DateTime<-ymd_hms(timesdf$DateTime,tz=Sys.timezone())
timesdf
head(timesdf)
#    DateTime
#1 2021-02-20
#2 2021-02-20
#3 2021-02-20
#4 2021-02-20
#5 2021-02-20
#6 2021-02-20



#POSIXct solution 1
timesdf$DateTime<-as.POSIXct(timesdf$DateTime, "%Y/%m/%d %H:%M:%OS")
#Warning messages:
#1: In strptime(xx, f, tz = tz) : unknown timezone '%Y/%m/%d %H:%M:%OS'
#2: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y/%m/%d %H:%M:%OS'
#3: In strptime(x, f, tz = tz) : unknown timezone '%Y/%m/%d %H:%M:%OS'
#4: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
#  unknown timezone '%Y/%m/%d %H:%M:%OS'
head(timesdf)
#    DateTime
#1 2021-02-20
#2 2021-02-20
#3 2021-02-20
#4 2021-02-20
#5 2021-02-20
#6 2021-02-20


#POSIXct solution 2
timesdf$DateTime<-as.POSIXct(timesdf$DateTime, "%Y-%m-%d %H:%M:%0S")
#Warning messages:
#  1: In strptime(xx, f, tz = tz) : unknown timezone '%Y-%m-%d %H:%M:%S'
#2: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-%m-%d %H:%M:%S'
#3: In strptime(x, f, tz = tz) : unknown timezone '%Y-%m-%d %H:%M:%S'
#4: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
#  unknown timezone '%Y-%m-%d %H:%M:%S'
head(timesdf)
#    DateTime
#1 2021-02-20
#2 2021-02-20
#3 2021-02-20
#4 2021-02-20
#5 2021-02-20
#6 2021-02-20

如您所见,时间戳的 H:M:S 部分已被删除,这不是我想要的。 我通常使用 lubridate 来标准化日期时间戳,如果(很少)失败,那么我使用 as.POSIXct 函数。但我不知道为什么要删除 H:M:S 部分。

这可能是一个重复的问题,但我没有发现任何与我的问题相似的明显问题。任何指针将不胜感激:)

正如评论中所指出的,问题在于数据是如何打印的。为了说服自己,只需尝试将 POSIXct:

创建的变量加 1
timesdf<-structure(list(DateTime = c("2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00", "2021-02-20 00:00:00", "2021-02-20 00:00:00", 
                                     "2021-02-20 00:00:00")), row.names = c(NA, 15L), class = "data.frame")

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

timesdf <- timesdf |> 
  mutate(times = as.POSIXct(DateTime))

head(timesdf)
#>              DateTime      times
#> 1 2021-02-20 00:00:00 2021-02-20
#> 2 2021-02-20 00:00:00 2021-02-20
#> 3 2021-02-20 00:00:00 2021-02-20
#> 4 2021-02-20 00:00:00 2021-02-20
#> 5 2021-02-20 00:00:00 2021-02-20
#> 6 2021-02-20 00:00:00 2021-02-20

timesdf |> 
  mutate(times = times + 1) |> 
  head()
#>              DateTime               times
#> 1 2021-02-20 00:00:00 2021-02-20 00:00:01
#> 2 2021-02-20 00:00:00 2021-02-20 00:00:01
#> 3 2021-02-20 00:00:00 2021-02-20 00:00:01
#> 4 2021-02-20 00:00:00 2021-02-20 00:00:01
#> 5 2021-02-20 00:00:00 2021-02-20 00:00:01
#> 6 2021-02-20 00:00:00 2021-02-20 00:00:01

reprex package (v2.0.1)

于 2021-09-16 创建

您从 POSIXct 命令(“未知时区”)中得到的错误是由于 POSIXct 函数的第二个参数是 tz,因为您从上面的代码中可以看出,您不必指定格式。