POSIX格式的日期和时间 [英] POSIX formatted Date and Time

查看:441
本文介绍了POSIX格式的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将csv文件中的内容读入R

I am reading this from csv file into R

df<-

ID   DATE         TIME
1    10/14/2000    8:30:05
2    02/13/2001    12:05:05

我在将其转换为POSIX格式的日期和时间时遇到麻烦.

I have trouble converting this to POSIX formatted date and time.

df <-
ID   DATE         TIME        DATETIMEPOSIX
1    10/14/2000    8:30:05    2000-10-14 8:30:05
2    02/13/2001    01:05:05   2001-02-13 13:05:05   

我已经尝试过了,但是得到了NAs

I have tried this but got NAs

df$DateTime <- paste(df$DATE, df$TIME)
df$DateTimePOSIX <- strptime(df$DateTime,  format = "%Y-%m-%d %H:%M:%S")

推荐答案

format参数必须是所读取内容的格式,而不是您希望输出的格式.

The format argument needs to be the format of what it's reading, not what you want the output to be.

此外,我假设您的日期部分位于美国版的"mm/dd/yyyy"中

Also, I assume your date component is in the American version of 'mm/dd/yyyy'

考虑

DateTime <- "10/14/2000 8:30:05"

as.POSIXct(DateTime, format = "%m/%d/%Y %H:%M:%S")
"2000-10-14 08:30:05 AEDT"

所以你想要

df$DateTimePOSIX <- as.POSIXct(df$DateTime,  format = "%m/%d/%Y %H:%M:%S")

df
#   ID       DATE     TIME            DateTime       DateTimePOSIX
# 1  1 10/14/2000  8:30:05  10/14/2000 8:30:05 2000-10-14 08:30:05
# 2  2 02/13/2001 12:05:05 02/13/2001 12:05:05 2001-02-13 12:05:05

这篇关于POSIX格式的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆