Translate

Monday, 13 October 2014

Date & Time

Date and Time formats and conversions in R


#as.Date
#yyyy/mm/dd
Date1<-c("2013/07/15","2013/08/13","2013/07/23","2014/01/31","2013/02/13","2014/07/13")
as.Date(Date1,format="%Y/%m/%d",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#yy/mm/dd
Date2<-c("13/07/15","13/08/13","13/07/23","14/01/31","13/02/13","14/07/13")
as.Date(Date2,format="%y/%m/%d",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#yy/m/dd
Date3<-c("15/7/13","13/8/13","23/7/13","14/1/31","13/2/13","14/7/13")
as.Date(Date3,format="%y/%m/%d",tz="Australia/Sydney")

[1] "2015-07-13" "2013-08-13" "2023-07-13" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#dd mmm yyyy
Date4<-c("15 jul 2013","13 aug 2013","23 jul 2013","31 jan 2014","13 Feb 2013",
"13 Jul 2014")
as.Date(Date4,format="%d %b %Y",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#dd month yyyy
Date5<-c("15 july 2013","13 August 2013","23 july 2013","31 january 2014",
"13 February 2013","13 July 2014")
as.Date(Date5,format="%d %B %Y",tz="Australia/Sydney")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#weekday  abbreviated
DateProper<-as.Date(Date5,format="%d %B %Y",tz="Australia/Sydney")
format(DateProper,"%a")

[1] "Mon" "Tue" "Tue" "Fri" "Wed" "Sun"


#weekday full
format(DateProper,"%A")

[1] "Monday"    "Tuesday"   "Tuesday"   "Friday"    "Wednesday" "Sunday"


#weekdays alternative
weekdays(DateProper)

[1] "Monday"    "Tuesday"   "Tuesday"   "Friday"    "Wednesday" "Sunday"


#ISO C99 date format
format(DateProper,"%D")

[1] "07/15/13" "08/13/13" "07/23/13" "01/31/14" "02/13/13" "07/13/14"


#ISO 8601 date format
format(DateProper,"%F")

[1] "2013-07-15" "2013-08-13" "2013-07-23" "2014-01-31" "2013-02-13"
[6] "2014-07-13"


#day of month
format(DateProper,"%e")

[1] "15" "13" "23" "31" "13" "13"


#day of year
format(DateProper,"%j")

[1] "196" "225" "204" "031" "044" "194"


#week of year where week 1 is first full week staring Sunday
format(DateProper,"%U")

[1] "28" "32" "29" "04" "06" "28"


#week of year where week 1 is first week containing 4 or more days of the new year
format(DateProper,"%V")

[1] "29" "33" "30" "05" "07" "28"


#week of year where week 1 is first full week staring Monday
format(DateProper,"%W")

[1] "28" "32" "29" "04" "06" "27"


#strptime
#24 hour format
TimeStamp1<-c("2013/07/15 13:10:23","2013/08/13 13:10:23",
"2013/07/23 19:10:25","2014/01/31 03:10:23","2013/02/13 13:50:53","2014/07/13 21:13:43")

strptime(TimeStamp1,format="%Y/%m/%d %H:%M:%S",tz="Australia/Sydney")


[1] "2013-07-15 13:10:23 EST" "2013-08-13 13:10:23 EST"
[3] "2013-07-23 19:10:25 EST" "2014-01-31 03:10:23 EST"
[5] "2013-02-13 13:50:53 EST" "2014-07-13 21:13:43 EST"



#AM/PM format
TimeStamp2<-c("2013/07/15 1:10:23 PM","2013/08/13 1:10:23 PM",
"2013/07/23 7:10:25 PM","2014/01/31 3:10:23 AM","2013/02/13 1:50:53 PM",
"2014/07/13 9:13:43 PM")

TSProper<-strptime(TimeStamp2,
format="%Y/%m/%d %I:%M:%S %p",tz="Australia/Sydney")

TSProper

[1] "2013-07-15 13:10:23 EST" "2013-08-13 13:10:23 EST"
[3] "2013-07-23 19:10:25 EST" "2014-01-31 03:10:23 EST"
[5] "2013-02-13 13:50:53 EST" "2014-07-13 21:13:43 EST"


#hour & minute AM/PM
format(TSProper,"%r")

[1] "01:10:23 PM" "01:10:23 PM" "07:10:25 PM" "03:10:23 AM" "01:50:53 PM"
[6] "09:13:43 PM"


#hour & minute AM/PM without leading zeros
format(TSProper,"%X")

[1] "1:10:23 PM" "1:10:23 PM" "7:10:25 PM" "3:10:23 AM" "1:50:53 PM"
[6] "9:13:43 PM"


#hour & minute
format(TSProper,"%R")

[1] "13:10" "13:10" "19:10" "03:10" "13:50" "21:13"


#hour, minute & second
format(TSProper,"%T")

[1] "13:10:23" "13:10:23" "19:10:25" "03:10:23" "13:50:53" "21:13:43"


#full
format(TSProper,"%c")

[1] "Mon Jul 15 13:10:23 2013" "Tue Aug 13 13:10:23 2013"
[3] "Tue Jul 23 19:10:25 2013" "Fri Jan 31 03:10:23 2014"
[5] "Wed Feb 13 13:50:53 2013" "Sun Jul 13 21:13:43 2014"



#system time
Sys.time()
  
[1] "2015-04-20 13:26:46 AEST"


#system date
Sys.Date()
  
[1] "2015-04-20"





No comments:

Post a Comment