简体   繁体   English

将 R 中的列转换为 datetime 类型:as.POSIXct 返回 NA

[英]Converting a column in R to type datetime: as.POSIXct returns NA

I am struggling while trying to convert a column of type char to datetime.我在尝试将 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

Using mutate:使用变异:

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

While the type for Date.Time has changed from to, the whole column is just NAs.虽然 Date.Time 的类型已从 to 更改,但整个列只是 NA。

Output of Sys.getlocale(): Sys.getlocale() 的 Output:

'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'

Your input formatting codes are not correct.您输入的格式代码不正确。 Should be "%m/%d/%Y %H:%M:%S" instead of "%M/%D%/%Y% %H:%M%S" .应该是"%m/%d/%Y %H:%M:%S"而不是"%M/%D%/%Y% %H:%M%S" Everything else is fine:其他一切都很好:

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" ...

You can use the library anytime to do this.您可以anytime使用该库来执行此操作。 Here is an example -这是一个例子 -

date_var = "4/30/2014"

library(anytime)
anytime::anydate(date_var)

results in结果是

> "2014-04-30"

An option with lubridate带有lubridate的选项

library(dplyr)
library(lubridate)
df %>% 
  mutate(Date.Time = mdy_hms(Date.Time))

Actually, your attempt is quite correct, only the format was wrong.实际上,您的尝试是非常正确的,只是格式错误。

as.POSIXct("4/1/2014 0:21:00", format = "%M/%D%/%Y% %H:%M%S")
#> [1] NA

If you remove the extra % after year and day, add the missing: between minutes and seconds and make the codes for year and month a lower case letter (because %M stands for minutes and %D is short for %m/%d/%y: :如果在年和日之后删除多余的 %,请在分钟和秒之间添加缺少的:,并将年和月的代码设为小写字母(因为%M代表分钟, %D%m/%d/%y:的缩写%m/%d/%y: :

as.POSIXct("4/1/2014 0:21:00", format = "%m/%d/%Y %H:%M:%S")
#> [1] "2014-04-01 00:21:00 CEST"

Created on 2021-02-14 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 2 月 14 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM