从日期和时间格式更改为数字格式 [英] Change from date and hour format to numeric format
问题描述
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屋!