Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

r - Change from date and hour format to numeric format

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...