从日期和时间格式更改为数字格式 [英] Change from date and hour format to numeric format

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

问题描述

我正在R工作,我需要从一个列格式更改

  9/27/2011 3:33 :00 PM 

到值格式。在Excel中,我可以使用函数 value(),但我不知道如何在R中执行。



我的资料如下所示:

  9/27/2011 15:33 a 1 5 9 
9/27 / 2011 15:33 v 2 6 2
9/27/2011 15:34 c 3 7 1


解决方案

要将字符串转换为R日期格式,请使用 as.POSIXct - 然后可以使用 as.numeric

 > x<  -  as.POSIXct(9/27/2011 3:33:00 PM,format =%m /%d /%Y%H:%M:%S%p)
> ; x
[1]2011-09-27 03:33:00 BST
> as.numeric(x)
[1] 1317090780

您获得的值表示数字从任意日期开始,通常为1/1/1970。请注意,这不同于Excel,其中一个日期被存储为自任意日期以来的天数(如果我的记忆非常适合我,我将尝试不再使用Excel)1/1/1900。



有关详细信息,请参阅?DateTimeClasses


I am working in R and I need to change from a column in format

9/27/2011  3:33:00 PM 

to a value format. In Excel I can use the function value() but I do not know how to do it in R.

My data looks like this:

9/27/2011 15:33 a   1   5   9
9/27/2011 15:33 v   2   6   2
9/27/2011 15:34 c   3   7   1

解决方案

To convert a string into R date format, use as.POSIXct - then you can coerce it to a numeric value using as.numeric:

> x <- as.POSIXct("9/27/2011  3:33:00 PM", format="%m/%d/%Y  %H:%M:%S %p")
> x
[1] "2011-09-27 03:33:00 BST"
> as.numeric(x)
[1] 1317090780

The value you get indicates the number of seconds since an arbitrary date, usually 1/1/1970. Note that this is different from Excel, where a date is stored as the number of days since an arbitrary date (1/1/1900 if my memory serves me well - I try not to use Excel any more.)

For more information, see ?DateTimeClasses

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

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