在数据框中分割日期和时间 [英] splitting date and time in data frame

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

问题描述

我在数据框中有日期的列列表,日期格式为 201001011200 作为%Y%m%d%H%M 。我想将它们拆分为%Y%m%d %H%M 作为日期和时间。

I have a column list of dates in data frame with date format 201001011200 as %Y%m%d%H%M. I wanted to split them as %Y%m%d and %H%M as Date and Time.

我尝试将 as.Date(data $ Date,origin = 1970-01-01)错误消息


charToDate(x)中的错误:字符串不是标准的
明确格式

Error in charToDate(x) : character string is not in a standard unambiguous format

日期的类别为数字。所以我试图将其转换为 character 并应用了上述 as.Date 函数,但没有帮助。

The class of the date is numeric. So I tried to convert it to characterand applied the above as.Date function but was not helpful.

有什么想法吗?

编辑

这里是我的数据示例:

Index Date          rank amount
81211 201004090000  11 4.9
81212 201004090100  11 4.6
81213 201004090200  11 3.3
81214 201004090300  11 2.7
81215 201004090400  11 3.1
81216 201004090500  11 3.7
81217 201004090600  11 4.0
81218 201004090700  11 4.2
81219 201004090800  11 4.2
81220 201004090900  11 4.0


推荐答案

更新后的答案:从示例数据开始,您可以

Updated Answer: Beginning with your example data, you can do

data$Date <- as.POSIXct(as.character(data$Date), format =  "%Y%m%d%H%M")

更改列到POSIX日期时间值。然后,要将日期和时间提取到两个单独的列中,您可以

to change the column to a POSIX datetime value. Then, to extract the date and time into two separate columns, you can do

data$date <- as.character(as.Date(data$Date))
data$time <- format(data$Date, "%T")

这将提供以下更新的数据框 data

This gives the following updated data frame data


   Index                Date rank amount       date     time
1  81211 2010-04-09 00:00:00   11    4.9 2010-04-09 00:00:00
2  81212 2010-04-09 01:00:00   11    4.6 2010-04-09 01:00:00
3  81213 2010-04-09 02:00:00   11    3.3 2010-04-09 02:00:00
4  81214 2010-04-09 03:00:00   11    2.7 2010-04-09 03:00:00
5  81215 2010-04-09 04:00:00   11    3.1 2010-04-09 04:00:00
6  81216 2010-04-09 05:00:00   11    3.7 2010-04-09 05:00:00
7  81217 2010-04-09 06:00:00   11    4.0 2010-04-09 06:00:00
8  81218 2010-04-09 07:00:00   11    4.2 2010-04-09 07:00:00
9  81219 2010-04-09 08:00:00   11    4.2 2010-04-09 08:00:00
10 81220 2010-04-09 09:00:00   11    4.0 2010-04-09 09:00:00







原始答案:如果您以数字开头,则将其包装在中.character(),然后通过 as.POSIXct()运行它以获取POSIX日期时间值。


Original Answer: If you are starting with a numeric value, wrap it in as.character() then run it through as.POSIXct() to get a POSIX date-time value.

data$Date <- as.POSIXct(as.character(data$Date), format = "%Y%m%d%H%M")

作为示例,我将使用 201001011200 作为

As an example I will use 201001011200 as you gave.

(x <- as.POSIXct(as.character(201001011200), format = "%Y%m%d%H%M"))
# [1] "2010-01-01 12:00:00 PST"

然后分开日期和时间,您可以执行以下操作。

Then to separate out the date and time you can do the following.

list(as.Date(x), format(x, "%T"))
# [[1]]
# [1] "2010-01-01"
# 
# [[2]]
# [1] "12:00:00"

分别给出日期和字符分类的项目。对于普通的旧字符向量,只需两次使用 format()

That gives Date and character classed items, respectively. For a plain old character vector, just use format() twice.

c(format(x, "%m-%d-%Y"), format(x, "%T"))
# [1] "01-01-2010" "12:00:00"  

c(as.character(as.Date(x)), format(x, "%T"))
# [1] "2010-01-01" "12:00:00"  

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

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