将因子转换为R中的DateTime

发布于 2025-01-29 14:40:32 字数 340 浏览 2 评论 0 原文

我有一个属于因子数据类型的柱面,但是时间戳以这种格式:

%y-%m-%dt%h:%m:%s

 $ fg_stop_time      : Factor w/ 8 levels "2022-05-16T20:38:19",..: 4 8

无法获得。

df$new_time <-strptime(x = as.character(df1$fg_stop_time), format = "%Y-%m-%d %H:%M:%S")

我似乎 之间。我该如何识别“ T”?

I have a colum that is of factor datatype, however the timestamp is in this format:

%Y-%m-%dT%H:%M:%S

 $ fg_stop_time      : Factor w/ 8 levels "2022-05-16T20:38:19",..: 4 8

I cant seem to get the as.character to work it keeps coming up as NA

df$new_time <-strptime(x = as.character(df1$fg_stop_time), format = "%Y-%m-%d %H:%M:%S")

I think it has to do with the fact that there is the T character in-between. How do I get it to recognize the 'T'?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2025-02-05 14:40:33

使用 lubridate 这怎么样?

x <- as.factor("2022-05-16T20:38:19")

library(lubridate)

y <- ymd_hms(x)

str(y)

posixct [1:1],格式:“ 2022-05-16 20:38:19”

How about this with lubridate ?

x <- as.factor("2022-05-16T20:38:19")

library(lubridate)

y <- ymd_hms(x)

str(y)

POSIXct[1:1], format: "2022-05-16 20:38:19"

澉约 2025-02-05 14:40:33

只需在格式中添加 t in Strptime as.posixct (最好使用 > as.posixct as Strptime 返回 list with Posixlt class

as.POSIXct("2022-05-16T20:38:19", format = "%FT%T")

Just add the T within the format in strptime or as.POSIXct (it may be better to use as.POSIXct as strptime returns a list with POSIXlt class

as.POSIXct("2022-05-16T20:38:19", format = "%FT%T")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文