{"id":859,"date":"2015-07-29T15:43:08","date_gmt":"2015-07-29T22:43:08","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=859"},"modified":"2015-07-29T15:43:08","modified_gmt":"2015-07-29T22:43:08","slug":"dates-and-times-in-r","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2015\/07\/29\/dates-and-times-in-r\/","title":{"rendered":"Dates and Times in R"},"content":{"rendered":"<h1 align=\"center\">Dates and Times in R<\/h1>\n<p>R provides several options for dealing with date and date\/time data. The builtin <tt>as.Date<\/tt> function handles dates (without times); the contributed library <tt>chron<\/tt> handles dates and times, but does not control for time zones; and the <tt>POSIXct<\/tt> and <tt>POSIXlt<\/tt> classes allow for dates and times with control for time zones. The general rule for date\/time data in R is to use the simplest technique possible. Thus, for date only data, <tt>as.Date<\/tt> will usually be the best choice. If you need to handle dates and times, without timezone information, the <tt>chron<\/tt> library is a good choice; the POSIX classes are especially useful when timezone manipulation is important. Also, don&#8217;t overlook the various &#8220;<tt>as.<\/tt>&#8221; functions (see Section\u00a0) for converting among the different date types when necessary.<\/p>\n<div class=\"p\"><\/div>\n<p>Except for the <tt>POSIXlt<\/tt> class, dates are stored internally as the number of days or seconds from some reference date. Thus dates in R will generally have a numeric mode, and the <tt>class<\/tt> function can be used to find the way they are actually being stored. The<tt>POSIXlt<\/tt> class stores date\/time values as a list of components (<tt>hour<\/tt>, <tt>min<\/tt>, <tt>sec<\/tt>, <tt>mon<\/tt>, etc.) making it easy to extract these parts.<\/p>\n<div class=\"p\"><\/div>\n<p>To get the current date, the Sys.Date function will return a <tt>Date<\/tt> object which can be converted to a different class if necessary.<\/p>\n<div class=\"p\"><\/div>\n<p>The following subsections will describe the different types of date values in more detail.<\/p>\n<div class=\"p\"><\/div>\n<p>The <tt>as.Date<\/tt> function allows a variety of input formats through the <tt>format=<\/tt> argument. The default format is a four digit year, followed by a month, then a day, separated by either dashes or slashes. The following example shows some examples of dates which <tt>as.Date<\/tt> will accept by default:<\/p>\n<pre>&gt;\u00a0as.Date('1915-6-16')\n[1]\u00a0\"1915-06-16\"\n&gt;\u00a0as.Date('1990\/02\/17')\n[1]\u00a0\"1990-02-17\"\n\n<\/pre>\n<div class=\"p\"><\/div>\n<p><a name=\"tth_tAb1\"><\/a><\/p>\n<p><center><\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>Code<\/td>\n<td>Value<\/td>\n<\/tr>\n<tr>\n<td><tt>%d<\/tt><\/td>\n<td>Day of the month (decimal number)<\/td>\n<\/tr>\n<tr>\n<td><tt>%m<\/tt><\/td>\n<td>Month (decimal number)<\/td>\n<\/tr>\n<tr>\n<td><tt>%b<\/tt><\/td>\n<td>Month (abbreviated)<\/td>\n<\/tr>\n<tr>\n<td><tt>%B<\/tt><\/td>\n<td>Month (full name)<\/td>\n<\/tr>\n<tr>\n<td><tt>%y<\/tt><\/td>\n<td>Year (2 digit)<\/td>\n<\/tr>\n<tr>\n<td><tt>%Y<\/tt><\/td>\n<td>Year (4 digit)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/center><\/p>\n<div class=\"p\"><\/div>\n<p>If your input dates are not in the standard format, a format string can be composed using the elements shown in Table\u00a0. The following examples show some ways that this can be used:<\/p>\n<pre>&gt;\u00a0as.Date('1\/15\/2001',format='%m\/%d\/%Y')\n[1]\u00a0\"2001-01-15\"\n&gt;\u00a0as.Date('April\u00a026,\u00a02001',format='%B\u00a0%d,\u00a0%Y')\n[1]\u00a0\"2001-04-26\"\n&gt;\u00a0as.Date('22JUN01',format='%d%b%y')\u00a0\u00a0\u00a0#\u00a0%y\u00a0is\u00a0system-specific;\u00a0use\u00a0with\u00a0caution\n[1]\u00a0\"2001-06-22\"\n\n<\/pre>\n<div class=\"p\"><\/div>\n<p>Internally, <tt>Date<\/tt> objects are stored as the number of days since January 1, 1970, using negative numbers for earlier dates. The <tt>as.numeric<\/tt> function can be used to convert a <tt>Date<\/tt> object to its internal form.<\/p>\n<div class=\"p\"><\/div>\n<p>To extract the components of the dates, the <tt>weekdays<\/tt>, <tt>months<\/tt>, <tt>days<\/tt> or <tt>quarters<\/tt> functions can be used. For example, to find the day of the week on which some famous statisticians were born, we can look at the result of the <tt>weekdays<\/tt> function:<\/p>\n<pre>&gt;\u00a0bdays\u00a0=\u00a0c(tukey=as.Date('1915-06-16'),fisher=as.Date('1890-02-17'),\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cramer=as.Date('1893-09-25'),\u00a0kendall=as.Date('1907-09-06'))\n&gt;\u00a0weekdays(bdays)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0tukey\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0fisher\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cramer\u00a0\u00a0\u00a0\u00a0\u00a0kendall\n\"Wednesday\"\u00a0\u00a0\u00a0\u00a0\"Monday\"\u00a0\u00a0\u00a0\u00a0\"Monday\"\u00a0\u00a0\u00a0\u00a0\"Friday\"\n\n<\/pre>\n<div class=\"p\"><\/div>\n<p>For an alternative way of extracting pieces of a day, and for information on possible output formats for <tt>Date<\/tt> objects, see Section\u00a0.<\/p>\n<div class=\"p\"><\/div>\n<p>The <tt>chron<\/tt> function converts dates and times to <tt>chron<\/tt> objects. The dates and times are provided to the <tt>chron<\/tt> function as separate values, so some preprocessing may be necessary to prepare input date\/times for the <tt>chron<\/tt> function. When using character values, the default format for dates is the decimal month value followed by the decimal day value followed by the year, using the slash as a separator. Alternative formats can be provided by using the codes shown in Table<\/p>\n<div class=\"p\"><\/div>\n<p><a name=\"tth_tAb1\"><\/a><\/p>\n<p><center><\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td colspan=\"2\" align=\"center\">Format codes for dates<\/td>\n<\/tr>\n<tr>\n<td>Code<\/td>\n<td>Value<\/td>\n<\/tr>\n<tr>\n<td><tt>m<\/tt><\/td>\n<td>Month (decimal number)<\/td>\n<\/tr>\n<tr>\n<td><tt>d<\/tt><\/td>\n<td>Day of the month (decimal number)<\/td>\n<\/tr>\n<tr>\n<td><tt>y<\/tt><\/td>\n<td>Year (4 digit)<\/td>\n<\/tr>\n<tr>\n<td><tt>mon<\/tt><\/td>\n<td>Month (abbreviated)<\/td>\n<\/tr>\n<tr>\n<td><tt>month<\/tt><\/td>\n<td>Month (full name)<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\" align=\"center\">Format codes for times<\/td>\n<\/tr>\n<tr>\n<td>Code<\/td>\n<td>Value<\/td>\n<\/tr>\n<tr>\n<td><tt>h<\/tt><\/td>\n<td>Hour<\/td>\n<\/tr>\n<tr>\n<td><tt>m<\/tt><\/td>\n<td>Minute<\/td>\n<\/tr>\n<tr>\n<td><tt>s<\/tt><\/td>\n<td>Second<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/center><\/p>\n<div class=\"p\"><\/div>\n<p>Alternatively, dates can be specified by a numeric value, representing the number of days since January 1, 1970. To input dates stored as the day of the year, the <tt>origin=<\/tt> argument can be used to interpret numeric dates relative to a different date.<\/p>\n<div class=\"p\"><\/div>\n<p>The default format for times consists of the hour, minutes and seconds, separated by colons. Alternative formats can use the codes in Table\u00a0.<\/p>\n<div class=\"p\"><\/div>\n<p>Often the first task when using the <tt>chron<\/tt> library is to break apart the date and times if they are stored together. In the following example, the <tt>strsplit<\/tt> function is used to break apart the string.<\/p>\n<pre>&gt;\u00a0dtimes\u00a0=\u00a0c(\"2002-06-09\u00a012:45:40\",\"2003-01-29\u00a009:30:40\",\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"2002-09-04\u00a016:45:40\",\"2002-11-13\u00a020:00:40\",\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"2002-07-07\u00a017:30:40\")\n&gt;\u00a0dtparts\u00a0=\u00a0t(as.data.frame(strsplit(dtimes,'\u00a0')))\n&gt;\u00a0row.names(dtparts)\u00a0=\u00a0NULL\n&gt;\u00a0thetimes\u00a0=\u00a0chron(dates=dtparts[,1],times=dtparts[,2],\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0format=c('y-m-d','h:m:s'))\n&gt;\u00a0thetimes\n[1]\u00a0(02-06-09\u00a012:45:40)\u00a0(03-01-29\u00a009:30:40)\u00a0(02-09-04\u00a016:45:40)\n[4]\u00a0(02-11-13\u00a020:00:40)\u00a0(02-07-07\u00a017:30:40)\n\n<\/pre>\n<p>Chron values are stored internally as the fractional number of days from January 1, 1970. The <tt>as.numeric<\/tt> function can be used to access the internal values.<\/p>\n<div class=\"p\"><\/div>\n<p>For information on formatting <tt>chron<\/tt> objects for output, see Section<\/p>\n<div class=\"p\"><\/div>\n<p>POSIX represents a portable operating system interface, primarily for UNIX systems, but available on other operating systems as well. Dates stored in the POSIX format are date\/time values (like dates with the <tt>chron<\/tt> library), but also allow modification of time zones. Unlike the <tt>chron<\/tt> library, which stores times as fractions of days, the POSIX date classes store times to the nearest second, so they provide a more accurate representation of times.<\/p>\n<div class=\"p\"><\/div>\n<p>There are two POSIX date\/time classes, which differ in the way that the values are stored internally. The <tt>POSIXct<\/tt> class stores date\/time values as the number of seconds since January 1, 1970, while the <tt>POSIXlt<\/tt> class stores them as a list with elements for second, minute, hour, day, month, and year, among others. Unless you need the list nature of the <tt>POSIXlt<\/tt> class, the <tt>POSIXct<\/tt> class is the usual choice for storing dates in R.<\/p>\n<div class=\"p\"><\/div>\n<p>The default input format for POSIX dates consists of the year, followed by the month and day, separated by slashes or dashes; for date\/time values, the date may be followed by white space and a time in the form hour:minutes:seconds or hour:minutes; thus, the following are examples of valid POSIX date or date\/time inputs:<\/p>\n<pre>1915\/6\/16\n2005-06-24\u00a011:25\n1990\/2\/17\u00a012:20:05\n\n<\/pre>\n<p>If the input times correspond to one of these formats, as.POSIXct can be called directly:<\/p>\n<pre>&gt;\u00a0dts\u00a0=\u00a0c(\"2005-10-21\u00a018:47:22\",\"2005-12-24\u00a016:39:58\",\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"2005-10-28\u00a007:30:05\u00a0PDT\")\n&gt;\u00a0as.POSIXlt(dts)\n[1]\u00a0\"2005-10-21\u00a018:47:22\"\u00a0\"2005-12-24\u00a016:39:58\"\u00a0\n[3]\u00a0\"2005-10-28\u00a007:30:05\"\n\n<\/pre>\n<p>If your input date\/times are stored as the number of seconds from January 1, 1970, you can create <tt>POSIX<\/tt> date values by assigning the appropriate class directly to those values. Since most date manipulation functions refer to the <tt>POSIXt<\/tt> psuedo-class, be sure to include it as the first member of the class attribute.<\/p>\n<pre>&gt;\u00a0dts\u00a0=\u00a0c(1127056501,1104295502,1129233601,1113547501,\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01119826801,1132519502,1125298801,1113289201)\n&gt;\u00a0mydates\u00a0=\u00a0dts\n&gt;\u00a0class(mydates)\u00a0=\u00a0c('POSIXt','POSIXct')\n&gt;\u00a0mydates\n[1]\u00a0\"2005-09-18\u00a008:15:01\u00a0PDT\"\u00a0\"2004-12-28\u00a020:45:02\u00a0PST\"\n[3]\u00a0\"2005-10-13\u00a013:00:01\u00a0PDT\"\u00a0\"2005-04-14\u00a023:45:01\u00a0PDT\"\n[5]\u00a0\"2005-06-26\u00a016:00:01\u00a0PDT\"\u00a0\"2005-11-20\u00a012:45:02\u00a0PST\"\n[7]\u00a0\"2005-08-29\u00a000:00:01\u00a0PDT\"\u00a0\"2005-04-12\u00a000:00:01\u00a0PDT\"\n\n<\/pre>\n<p>Conversions like this can be done more succinctly using the <tt>structure<\/tt> function:<\/p>\n<pre>&gt;\u00a0mydates\u00a0=\u00a0structure(dts,class=c('POSIXt','POSIXct'))\n\n<\/pre>\n<div class=\"p\"><\/div>\n<div class=\"p\"><\/div>\n<p><a name=\"tth_tAb1\"><\/a><\/p>\n<p><center><\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>Code<\/td>\n<td>Meaning<\/td>\n<td>Code<\/td>\n<td>Meaning<\/td>\n<\/tr>\n<tr>\n<td><tt>%a<\/tt><\/td>\n<td>Abbreviated weekday<\/td>\n<td><tt>%A<\/tt><\/td>\n<td>Full weekday<\/td>\n<\/tr>\n<tr>\n<td><tt>%b<\/tt><\/td>\n<td>Abbreviated month<\/td>\n<td><tt>%B<\/tt><\/td>\n<td>Full month<\/td>\n<\/tr>\n<tr>\n<td><tt>%c<\/tt><\/td>\n<td>Locale-specific date and time<\/td>\n<td><tt>%d<\/tt><\/td>\n<td>Decimal date<\/td>\n<\/tr>\n<tr>\n<td><tt>%H<\/tt><\/td>\n<td>Decimal hours (24 hour)<\/td>\n<td><tt>%I<\/tt><\/td>\n<td>Decimal hours (12 hour)<\/td>\n<\/tr>\n<tr>\n<td><tt>%j<\/tt><\/td>\n<td>Decimal day of the year<\/td>\n<td><tt>%m<\/tt><\/td>\n<td>Decimal month<\/td>\n<\/tr>\n<tr>\n<td><tt>%M<\/tt><\/td>\n<td>Decimal minute<\/td>\n<td><tt>%p<\/tt><\/td>\n<td>Locale-specific AM\/PM<\/td>\n<\/tr>\n<tr>\n<td><tt>%S<\/tt><\/td>\n<td>Decimal second<\/td>\n<td><tt>%U<\/tt><\/td>\n<td>Decimal week of the year (starting on Sunday)<\/td>\n<\/tr>\n<tr>\n<td><tt>%w<\/tt><\/td>\n<td>Decimal Weekday (0=Sunday)<\/td>\n<td><tt>%W<\/tt><\/td>\n<td>Decimal week of the year (starting on Monday)<\/td>\n<\/tr>\n<tr>\n<td><tt>%x<\/tt><\/td>\n<td>Locale-specific Date<\/td>\n<td><tt>%X<\/tt><\/td>\n<td>Locale-specific Time<\/td>\n<\/tr>\n<tr>\n<td><tt>%y<\/tt><\/td>\n<td>2-digit year<\/td>\n<td><tt>%Y<\/tt><\/td>\n<td>4-digit year<\/td>\n<\/tr>\n<tr>\n<td><tt>%z<\/tt><\/td>\n<td>Offset from GMT<\/td>\n<td><tt>%Z<\/tt><\/td>\n<td>Time zone (character)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/center><\/p>\n<div class=\"p\"><\/div>\n<p>The POSIX date\/time classes take advantage of the POSIX date\/time implementation of your operating system, allowing dates and times in R to be manipulated in the same way they would in, for example a C program. The two most important functions in this regard are <tt>strptime<\/tt>, for inputting dates, and <tt>strftime<\/tt>, for formatting dates for output. Both of these functions use a variety of formatting codes, some of which are listed in Table\u00a0, to specify the way dates are read or printed. For example, dates in many logfiles are printed in a format like &#8220;<tt>16\/Oct\/2005:07:51:00<\/tt>&#8220;. To create a <tt>POSIXct<\/tt> date from a date in this format, the following call to <tt>strptime<\/tt> could be used:<\/p>\n<pre>&gt;\u00a0mydate\u00a0=\u00a0strptime('16\/Oct\/2005:07:51:00',format='%d\/%b\/%Y:%H:%M:%S')\n[1]\u00a0\"2005-10-16\u00a007:51:00\"\n\n<\/pre>\n<p>Note that non-format characters (like the slashes) are interpreted literally.<\/p>\n<div class=\"p\"><\/div>\n<p>When using <tt>strptime<\/tt>, an optional time zone can be specified with the <tt>tz=<\/tt> option.<\/p>\n<div class=\"p\"><\/div>\n<p>Another way to create POSIX dates is to pass the individual components of the time to the ISOdate function. Thus, the first date\/time value in the previous example could also be created with a call to <tt>ISOdate<\/tt>;<\/p>\n<pre>&gt;\u00a0ISOdate(2005,10,21,18,47,22,tz=\"PDT\")\n[1]\u00a0\"2005-10-21\u00a018:47:22\u00a0PDT\"\n\n<\/pre>\n<div class=\"p\"><\/div>\n<p>For formatting dates for output, the <tt>format<\/tt> function will recognize the type of your input date, and perform any necessary conversions before calling <tt>strftime<\/tt>, so <tt>strftime<\/tt> rarely needs to be called directly. For example, to print a date\/time value in an extended format, we could use:<\/p>\n<pre>&gt;\u00a0thedate\u00a0=\u00a0ISOdate(2005,10,21,18,47,22,tz=\"PDT\")\n&gt;\u00a0format(thedate,'%A,\u00a0%B\u00a0%d,\u00a0%Y\u00a0%H:%M:%S')\n[1]\u00a0\"Friday,\u00a0October\u00a021,\u00a02005\u00a018:47:22\"\n\n<\/pre>\n<p>When using POSIX dates, the optional <tt>usetz=TRUE<\/tt> argument to the <tt>format<\/tt> function can be specified to indicate that the time zone should be displayed.<\/p>\n<div class=\"p\"><\/div>\n<p>Additionally, <tt>as.POSIXlt<\/tt> and <tt>as.POSIXct<\/tt> can also accept <tt>Date<\/tt> or <tt>chron<\/tt> objects, so they can be input as described in the previous sections and converted as needed. Conversion between the two POSIX forms is also possible.<\/p>\n<div class=\"p\"><\/div>\n<p>The individual components of a POSIX date\/time object can be extracted by first converting to <tt>POSIXlt<\/tt> if necessary, and then accessing the components directly:<\/p>\n<pre>&gt;\u00a0mydate\u00a0=\u00a0as.POSIXlt('2005-4-19\u00a07:01:00')\n&gt;\u00a0names(mydate)\n[1]\u00a0\"sec\"\u00a0\u00a0\u00a0\"min\"\u00a0\u00a0\u00a0\"hour\"\u00a0\u00a0\"mday\"\u00a0\u00a0\"mon\"\u00a0\u00a0\u00a0\"year\"\u00a0\u00a0\n[7]\u00a0\"wday\"\u00a0\u00a0\"yday\"\u00a0\u00a0\"isdst\"\n&gt;\u00a0mydate$mday\n[1]\u00a019\n\n<\/pre>\n<div class=\"p\"><\/div>\n<p>Many of the statistical summary functions, like <tt>mean<\/tt>, <tt>min<\/tt>, <tt>max<\/tt>, etc are able to transparently handle date objects. For example, consider the release dates of various versions or R from 1.0 to 2.0:<\/p>\n<pre>&gt;\u00a0rdates\u00a0=\u00a0scan(what=\"\")\n1:\u00a01.0\u00a029Feb2000\n3:\u00a01.1\u00a015Jun2000\n5:\u00a01.2\u00a015Dec2000\n7:\u00a01.3\u00a022Jun2001\n9:\u00a01.4\u00a019Dec2001\n11:\u00a01.5\u00a029Apr2002\n13:\u00a01.6\u00a01Oct2002\n15:\u00a01.7\u00a016Apr2003\n17:\u00a01.8\u00a08Oct2003\n19:\u00a01.9\u00a012Apr2004\n21:\u00a02.0\u00a04Oct2004\n23:\nRead\u00a022\u00a0items\n&gt;\u00a0rdates\u00a0=\u00a0as.data.frame(matrix(rdates,ncol=2,byrow=TRUE))\n&gt;\u00a0rdates[,2]\u00a0=\u00a0as.Date(rdates[,2],format='%d%b%Y')\n&gt;\u00a0names(rdates)\u00a0=\u00a0c(\"Release\",\"Date\")\n&gt;\u00a0rdates\n\u00a0\u00a0\u00a0Release\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Date\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.0\u00a02000-02-29\n2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.1\u00a02000-06-15\n3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.2\u00a02000-12-15\n4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.3\u00a02001-06-22\n5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.4\u00a02001-12-19\n6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.5\u00a02002-04-29\n7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.6\u00a02002-10-01\n8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.7\u00a02003-04-16\n9\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.8\u00a02003-10-08\n10\u00a0\u00a0\u00a0\u00a0\u00a01.9\u00a02004-04-12\n11\u00a0\u00a0\u00a0\u00a0\u00a02.0\u00a02004-10-04\n\n<\/pre>\n<div class=\"p\"><\/div>\n<p>Once the dates are properly read into R, a variety of calculations can be performed:<\/p>\n<pre>&gt;\u00a0mean(rdates$Date)\n[1]\u00a0\"2002-05-19\"\n&gt;\u00a0range(rdates$Date)\n[1]\u00a0\"2000-02-29\"\u00a0\"2004-10-04\"\n&gt;\u00a0rdates$Date[11]\u00a0-\u00a0rdates$Date[1]\nTime\u00a0difference\u00a0of\u00a01679\u00a0days\n\n<\/pre>\n<div class=\"p\"><\/div>\n<p>If two times (using any of the date or date\/time classes) are subtracted, R will return the results in the form of a time difference, which represents a <tt>difftime<\/tt> object. For example, New York City experienced a major blackout on July 13, 1997, and another on August 14, 2003. To calculate the time interval between the two blackouts, we can simply subtract the two dates, using any of the classes that have been introduced:<\/p>\n<pre>&gt;\u00a0b1\u00a0=\u00a0ISOdate(1977,7,13)\n&gt;\u00a0b2\u00a0=\u00a0ISOdate(2003,8,14)\n&gt;\u00a0b2\u00a0-\u00a0b1\nTime\u00a0difference\u00a0of\u00a09528\u00a0days\n\n<\/pre>\n<p>If an alternative unit of time was desired, the <tt>difftime<\/tt> function could be called, using the optional <tt>units=<\/tt> argument can be used with any of the following values: &#8220;<tt>auto<\/tt>&#8220;, &#8220;<tt>secs<\/tt>&#8220;, &#8220;<tt>mins<\/tt>&#8220;, &#8220;<tt>hours<\/tt>&#8220;, &#8220;<tt>days<\/tt>&#8220;, or &#8220;<tt>weeks<\/tt>&#8220;. So to see the difference between blackouts in terms of weeks, we can use:<\/p>\n<pre>&gt;\u00a0difftime(b2,b1,units='weeks')\nTime\u00a0difference\u00a0of\u00a01361.143\u00a0weeks\n\n<\/pre>\n<p>Although <tt>difftime<\/tt> values are displayed with their units, they can be manipulated like ordinary numeric variables; arithmetic performed with these values will retain the original units.<\/p>\n<div class=\"p\"><\/div>\n<p>The <tt>by=<\/tt> argument to the <tt>seq<\/tt> function can be specified either as a <tt>difftime<\/tt> value, or in any units of time that the <tt>difftime<\/tt> function accepts, making it very easy to generate sequences of dates. For example, to generate a vector of ten dates, starting on July 4, 1976 with an interval of one day between them, we could use:<\/p>\n<pre>&gt;\u00a0seq(as.Date('1976-7-4'),by='days',length=10)\n\u00a0[1]\u00a0\"1976-07-04\"\u00a0\"1976-07-05\"\u00a0\"1976-07-06\"\u00a0\"1976-07-07\"\u00a0\"1976-07-08\"\n\u00a0[6]\u00a0\"1976-07-09\"\u00a0\"1976-07-10\"\u00a0\"1976-07-11\"\u00a0\"1976-07-12\"\u00a0\"1976-07-13\"\n\n<\/pre>\n<p>All the date classes except for <tt>chron<\/tt> will accept an integer before the interval provided as a <tt>by=<\/tt> argument. We could create a sequence of dates separated by two weeks from June 1, 2000 to August 1, 2000 as follows:<\/p>\n<pre>&gt;\u00a0seq(as.Date('2000-6-1'),to=as.Date('2000-8-1'),by='2\u00a0weeks')\n[1]\u00a0\"2000-06-01\"\u00a0\"2000-06-15\"\u00a0\"2000-06-29\"\u00a0\"2000-07-13\"\u00a0\"2000-07-27\"\n\n<\/pre>\n<p>The <tt>c<\/tt>ut function also understands units of <tt>days<\/tt>, <tt>weeks<\/tt>, <tt>months<\/tt>, and <tt>years<\/tt>, making it very easy to create factors grouped by these units. See Section\u00a0 for details.<\/p>\n<div class=\"p\"><\/div>\n<p>Format codes can also be used to extract parts of dates, similar to the <tt>weekdays<\/tt> and other functions described in Section\u00a0. We could look at the distribution of weekdays for the R release dates as follows:<\/p>\n<pre>&gt;\u00a0table(format(rdates$Date,'%A'))\n\n\u00a0\u00a0\u00a0Friday\u00a0\u00a0\u00a0\u00a0Monday\u00a0\u00a0Thursday\u00a0\u00a0\u00a0Tuesday\u00a0Wednesday\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a03\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a03\n\n<\/pre>\n<p>This same technique can be used to convert dates to factors. For example, to create a factor based on the release dates broken down by years we could use:<\/p>\n<pre>&gt;\u00a0fdate\u00a0=\u00a0factor(format(rdates$Date,'%Y'))\n&gt;\u00a0fdate\n\u00a0[1]\u00a02000\u00a02000\u00a02000\u00a02001\u00a02001\u00a02002\u00a02002\u00a02003\u00a02003\u00a02004\u00a02004\nLevels:\u00a02000\u00a02001\u00a02002\u00a02003\u00a02004\n\n<\/pre>\n<div class=\"p\"><\/div>\n<pre>&gt;\u00a0cut(thetimes,\"year\")\n[1]\u00a002\u00a003\u00a002\u00a002\u00a002\nLevels:\u00a002\u00a0&lt;\u00a003\n\n<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p><small>File translated from T<sub>E<\/sub>X by <a href=\"http:\/\/hutchinson.belmont.ma.us\/tth\/\">T<sub>T<\/sub>H<\/a>, version 3.67.<br \/>\nOn 3 Feb 2006, 17:06.<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dates and Times in R R provides several options for dealing with date and date\/time data. The builtin as.Date function handles dates (without times); the&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-859","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=859"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/859\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}