gpt4 book ai didi

r - 将 R 中的列转换为日期时间类型 : as. POSIXct 返回 NA

转载 作者:行者123 更新时间:2023-12-03 08:27:43 25 4
gpt4 key购买 nike

我在尝试将 char 类型的列转换为日期时间时遇到了困难。

 Date.Time  Lat Lon Base
<chr> <dbl> <dbl> <chr>
4/1/2014 0:11:00 40.7690 -73.9549 B02512
4/1/2014 0:17:00 40.7267 -74.0345 B02512
4/1/2014 0:21:00 40.7316 -73.9873 B02512
⋮ ⋮ ⋮ ⋮
4/30/2014 23:31:00 40.7443 -73.9889 B02764
4/30/2014 23:32:00 40.6756 -73.9405 B02764
4/30/2014 23:48:00 40.6880 -73.9608 B02764

使用变异:

df <- mutate(df, Date.Time = as.POSIXct(Date.Time,
format = "%M/%D%/%Y% %H:%M%S"))

虽然 Date.Time 的类型已从 更改为 ,但整个列只是 NA。

Sys.getlocale() 的输出:

'LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=C;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C'

最佳答案

您输入的格式代码不正确。应为 "%m/%d/%Y %H:%M:%S" 而不是 "%M/%D%/%Y% %H:%M%S “。其他一切都很好:

library(dplyr)

df <- read.table(header = TRUE, text = '
Date.Time Lat Lon Base
"4/1/2014 0:11:00" 40.7690 -73.9549 B02512
"4/1/2014 0:17:00" 40.7267 -74.0345 B02512
"4/1/2014 0:21:00" 40.7316 -73.9873 B02512
"4/30/2014 23:31:00" 40.7443 -73.9889 B02764
"4/30/2014 23:32:00" 40.6756 -73.9405 B02764
"4/30/2014 23:48:00" 40.6880 -73.9608 B02764')

df2 <- mutate(df, Date.Time = as.POSIXct(Date.Time,
format = "%m/%d/%Y %H:%M:%S"))

str(df2)
# 'data.frame': 6 obs. of 4 variables:
# $ Date.Time: POSIXct, format: "2014-04-01 00:11:00" "2014-04-01 00:17:00" "2014-04-01 00:21:00" ...
# $ Lat : num 40.8 40.7 40.7 40.7 40.7 ...
# $ Lon : num -74 -74 -74 -74 -73.9 ...
# $ Base : chr "B02512" "B02512" "B02512" "B02764" ...

关于r - 将 R 中的列转换为日期时间类型 : as. POSIXct 返回 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66197990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com