R Date and Time


R has serveral date and time related functions. date() functions returns a date without time as character string. Sys.Date() and Sys.time() returns the system's date and time as a Date and POSIXlt/POSIXct object respectively.

>date()

[1] "Fri Jan 04 17:38:05 2013"

>Sys.time()

[1] "2013-01-04 17:47:39 EST"

>Sys.Date()

[1] "2013-01-04"

>class(date())

[1] "character"

>class(Sys.Date())

[1] "Date"

>class(Sys.time())

[1] "POSIXct" "POSIXt"


POSIXct contains seconds from 1970-01-01.
POSIXlt is a list, contains:

sec
0-61: seconds
min
0-59: minutes
hour
0-23: hours
mday
1-31: day of the month
mon
0-11: months after the first of the year
year
years since 1900
wday
0-6: day of the week
yday
0-365: day of the year
isdst
Daylight savings time flag


>x <- "19:18:05"
>y <- strptime(x,"%H:%M:%S")
>y

[1] "2013-01-04 19:18:05"

>class(y)

[1] "POSIXlt" "POSIXt"

>y$sec

[1] 5


To convert the date and time with different format:

> t <- Sys.time()
> format(t, "%Y-%m-%d")
[1] "2020-01-29"

difftime() can be used to calculate time differences.

> difftime("2020-1-2","2012-5-4",units="weeks")
Time difference of 399.8631 weeks
> difftime("2020-1-2","2012-5-4",units="hours")
Time difference of 67177 hours






R date time format:
Syntax
Description
%a
Abbreviated weekday name in the current locale. (Also matches full name on input.)
%A
Full weekday name in the current locale. (Also matches abbreviated name on input.)
%b
Abbreviated month name in the current locale. (Also matches full name on input.)
%B
Full month name in the current locale. (Also matches abbreviated name on input.)
%c
Date and time. Locale-specific on output, "%a %b %e %H:%M:%S %Y" on input.
%d
Day of the month as decimal number (01-31).
%H
Hours as decimal number (00-23). As a special exception times such as 24:00:00 are accepted for input, since ISO 8601 allows these.
%I
Hours as decimal number (01-12).
%j
Day of year as decimal number (001-366).
%m
Month as decimal number (01-12).
%M
Minute as decimal number (00-59).
%p
AM/PM indicator in the locale. Used in conjunction with %I and not with %H. An empty string in some locales.
%S
Second as decimal number (00-61), allowing for up to two leap-seconds (but POSIX-compliant implementations will ignore leap seconds).
%U
Week of the year as decimal number (00-53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.
%w
Weekday as decimal number (0-6, Sunday is 0).
%W
Week of the year as decimal number (00-53) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1). The UK convention.
%x
Date. Locale-specific on output, "%y/%m/%d" on input.
%X
Time. Locale-specific on output, "%H:%M:%S" on input.
%y
Year without century (00-99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 - that is the behaviour specified by the 2004 and 2008 POSIX standards, but they do also say "it is expected that in a future version the default century inferred from a 2-digit year will change".
%Y
Year with century. Note that whereas there was no zero in the original Gregorian calendar, ISO 8601:2004 defines it to be valid (interpreted as 1BC): see http://en.wikipedia.org/wiki/0_(year). Note that the standard also says that years before 1582 in its calendar should only be used with agreement of the parties involved.
%z
Signed offset in hours and minutes from UTC, so -0800 is 8 hours behind UTC.
%Z
(output only.) Time zone as a character string (empty if not available). Where leading zeros are shown they will be used on output but are optional on input. Note that when %z or %Z is used for output with an object with an assigned timezone an attempt is made to use the values for that timezone, but it is not guaranteed to succeed.


endmemo.com © 2024  | Terms of Use | Privacy | Home