Changing the date and time format in digital format

I work in R and I need to switch from a column in the format

9/27/2011 3:33:00 PM 

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

My data is as follows:

 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 
+7
source share
2 answers

To convert a string to an R date format, use as.POSIXct - then you can force 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 from an arbitrary date, usually 1/1/1970. Please note that this is different from Excel, when the date is stored as the number of days from an arbitrary date (1/1/1900, if my memory helps me well - I no longer try to use Excel).

For more information see ?DateTimeClasses

+21
source

This was useful to me:

 > test=as.POSIXlt("09/13/2006", format="%m/%d/%Y") > test [1] "2006-09-13" > 1900+test$year [1] 2006 > test$yday [1] 255 > test$yday/365 [1] 0.6986301 > 1900+test$year+test$yday/366 [1] 2006.697 

You can use similar approaches if you need daily numbers, for example, in Excel.

+3
source

All Articles