尝试使用 as_datetime() 将字符转换为日期时间,但在 R 中格式错误
Trying to convert character to datetime using as_datetime() but getting a wrong format in R
我遇到了将 started_at 和 ended_at 字段正确转换为 datatime
格式的问题。当我使用 as_datetime()
时,年份从 2021 年更改为 2001 年。帮助。
作为字符的初始数据:01-07-2020 00:00
使用后
total_trips$started_at <- as_datetime(total_trips$started_at)
我得到 2001-07-20 20:00:00
尝试使用
total_trips$started_at <- as_datetime(total_trips$started_at, "%m%d%Y %H%m")
我收到以下错误:
Error in C_force_tz(time, tz = tzone, roll) : CCTZ: Unrecognized output timezone: "%d%m%Y %H:%M"
怎么了??
您应该更改格式以匹配您的数据,您的数据由破折号 (-) 分隔,因此正确的格式应该是 %d-%m-%Y %R
或 %d-%m-%Y %H:%M
。请注意 %R
等同于 %H:%M
.
library(lubridate)
as_datetime("01-07-2020 00:00", format = "%d-%m-%Y %R")
#> [1] "2020-07-01 UTC"
as_datetime("01-07-2020 17:59", format = "%d-%m-%Y %H:%M")
#> [1] "2020-07-01 17:59:00 UTC"
详细了解日期时间转换格式here
我遇到了将 started_at 和 ended_at 字段正确转换为 datatime
格式的问题。当我使用 as_datetime()
时,年份从 2021 年更改为 2001 年。帮助。
作为字符的初始数据:01-07-2020 00:00
使用后
total_trips$started_at <- as_datetime(total_trips$started_at)
我得到 2001-07-20 20:00:00
尝试使用
total_trips$started_at <- as_datetime(total_trips$started_at, "%m%d%Y %H%m")
我收到以下错误:
Error in C_force_tz(time, tz = tzone, roll) : CCTZ: Unrecognized output timezone: "%d%m%Y %H:%M"
怎么了??
您应该更改格式以匹配您的数据,您的数据由破折号 (-) 分隔,因此正确的格式应该是 %d-%m-%Y %R
或 %d-%m-%Y %H:%M
。请注意 %R
等同于 %H:%M
.
library(lubridate)
as_datetime("01-07-2020 00:00", format = "%d-%m-%Y %R")
#> [1] "2020-07-01 UTC"
as_datetime("01-07-2020 17:59", format = "%d-%m-%Y %H:%M")
#> [1] "2020-07-01 17:59:00 UTC"
详细了解日期时间转换格式here