• No results found

Functions, Units, and Codes

In document RProgramming_LearnR (Page 187-192)

R allows you to use various date functions and units to calculate the differences between dates, create sequences, and manipulate various dates and times. In this section, you will learn about functions such as the strptime(), strftime(), difftime(), POSIX, seq(), cut() and many other functions. You will also learn about the different units and codes used in these functions.

Strptime() and Strftime()

The two most essential functions used to manipulate date and time are the strptime() and strftime() functions. The strptime() function is used for entering dates and the strftime() function is for formatting date outputs. They use the different formatting codes specified in the above table. They specify how dates and times read and outputted.

If you would like to create a POSIXct date from this format, you would use the strptime() function.

The following example will show you how to use the strptime() function.

//The strptime() specifies the formatting code for a specific date and time

> mydate <- strptime('16/Oct/2005:07:51:00',format='%d/%b/%Y:%H:%M:%S') [1] "2005-10-16 07:51:00"

Notice that the non-format characters are specified literally with the use of backslashes. When you use the strptime() function in R, you will have the option to use the time zone “tz=” argument. Another way that you can use the POSIX date is to pass each elements of the time to the ISOdate() function.

Therefore, the initial date/time value in the example can be created with the ISOdate() function. The following example will show you how this is done:

//The date and time elements are passed

> ISOdate(2005,10,21,18,47,22,tz="PDT") [1] "2005-10-21 18:47:22 PDT"

If you would like to format dates for outputs, the format() function is used to identify the type of input date. It will then perform conversions before implementing the strftime() function. This function does not need to be called directly. If you have to print a date/time value for example, you could implement the ISOdate() function by doing the following:

// The format() function identifies the type of input date

> thedate <- ISOdate(2005,10,21,18,47,22,tz="PDT") > format(thedate,'%A, %B %d, %Y %H:%M:%S') [1] "Friday, October 21, 2005 18:47:22"

POSIX Functions

The POSIX date formats allows you to optionally use the “usetz=TRUE” argument with the format() function. It specifies that the time zone can be displayed.

There is also the “as.POSIXlt()” and the “as.POSIXct()” functions that allows you to use the “Date”

and “chron” objects. Therefore you can enter and convert them if necessary. You can also convert between the two POSIX formats. You can extract individual elements with the POSIX date/time object by initially converting to the “POSIXlt()” function and then accessing the elements.

Here is how you would implement the conversion with the POSIXlt() function.

//The POSIXlt() converts a specific date

> mydatetime <- as.POSIXlt('2005-4-19 7:01:00')

> names(mydate)

[1] "sec" "min" "hour" "mday" "mon" "year"

[7] "wday" "yday" "isdst"

> mydatetime$mday [1] 19

Summary Functions

R contains many statistical summary functions, such as “mean()”, “min()”, and “max()” functions.

They allow to handle date objects. For example if you need to know the release dates of various versions of R from versions 1.0 to 2.0, you could implement something like the following example:

// The version dates are read with the scan() function

> resdates <- scan(what="")

1: 1.0 29Feb2000 3: 1.1 15Jun2000 5: 1.2 15Dec2000 7: 1.3 22Jun2001 9: 1.4 19Dec2001 11: 1.5 29Apr2002 13: 1.6 1Oct2002 15: 1.7 16Apr2003 17: 1.8 8Oct2003 19: 1.9 12Apr2004 21: 2.0 4Oct2004 23: Read 22 items

// Columns and rows are created for the release dates. A specific format is specified.

> resdates = as.data.frame(matrix(resdates,ncol=2,byrow=TRUE))

> resdates[,2] = as.Date(resdates[,2],format='%d%b%Y') > names(resdates) = c("Version","Release Date")

// Displays the versions and release dates

> resdates

> Version Release Date 1.0 2000-02-29 1.1 2000-06-15

1.2 2000-12-15 etc...

When the dates are read correctly read into R, you can conduct the following calculations with the summary functions:

// The mean() function returns a specific date

> mean(rdates$Date) [1] "2002-05-19"

// The range() function calculates range of dates

> range(rdates$Date)

[1] "2000-02-29" "2004-10-04"

> rdates$Date[11] - rdates$Date[1]

Time difference of 1679 days

If you subtract with the date or date/time classes, R will return a time difference representing the

“difftime” object. For example, if a computer system experienced a system failure on January 13, 2013, and another system failure occurred on November 07, 2014, you can calculate the time interval between the two system failures, by subtracting the two dates with the ISOdate() function. The ISOdate() function is used in the following example to calculate the difference between the dates:

// The ISODate() function calculates the time difference in days.

> fail1 <- ISOdate(2013,1,13)

> fail2 <- ISOdate(2014,11,7)

> fail2 - fail1

Time difference of 663 days

Difftime() Function

If you would like to calculate an alternative unit of time, you could call the difftime() function, by optionally including the “units=” argument. The argument can use the “auto”, “secs”, “mins”,

“hours”, “days”, and “weeks” values to perform various date calculations.

The following example will show you how to use “difftime()” function to calculate a specific time difference:

// The difftime() function calculates the time difference in weeks

> difftime(fail2, fail1, units='weeks') Time difference of 94.71429 weeks

Values resulting from the “difftime()” function are displayed in units. You can manipulate them in the same way as numeric variables and still maintain the original units.

Seq() Function

You can implement the “by=” argument in the “seq()” function to mean the same as a difftime value or any units of time that is allows by the difftime() function. It makes it easier to create sequences of dates. The following example will show you how to use the “by=” argument to generate a vector of five dates, starting from August 10, 2013, with only an interval of one day between them:

// The seq() function returns a sequence of five days.

> seq(as.Date('2013-08-10'), by='days',length=5)

[1] "2013-08-10" "2013-08-11" "2013-08-12" "2013-08-13" "2013-08-14"

All the date classes will accept integer values before the “by=” argument. Only “chron” will not accept it. You could create a sequence of dates for every three weeks from September 10, 2014 to December 12, 2014. The sequence example maybe implemented as follows:

//The seq() function returns specific dates for every three weeks.

> seq(as.Date('2014-09-10'),to=as.Date('2014-12-12'), by='3 weeks') [1] "2014-09-10" "2014-10-01" "2014-10-22" "2014-11-12" "2014-12-03"

Date functions

As discussed earlier, date values are represented as the number of day since January 1, 1970, using negative values for earlier dates.

as.Date() function - The as.Date() allows you to convert character strings to date. The syntax for this function is as.Date(x, “format”), where x is the character date and “format” is the appropriate date format.

The following example will show you how to use the as.Date() function:

// The as.Date() converts strings to dates.

mydates <- as.Date(c("1971-10-22", "1973-11-07"))

//The number of days between 10/22/1971 and 11/07/1973 days <- mydates[1] - mydates[2]

Here is another example of how you can use the as.Date() function:

// Converts the date information to the format 'mm/dd/yyyy' strDates <- c("01/05/1965", "08/16/1975")

dates <- as.Date(strDates, "%m/%d/%Y")

Sys.Date() function – The “Sys.Date()” function returns the current date. The date() function on the other hand returns both the current date and time.

as.Character() function – The “as.Character()” function allows you to convert dates to characters. The following example will show you how to apply this function.

// Convert dates to character data

> strDates <- as.character(dates)

Units and Codes

The “cut()” function also allows you to use various units like “days”, “weeks” “months”, and

“years”. They make it simple for you to create factors with these units.

R also provides the format() function for outputting specific sections of dates. They are similar to the

“weekdays” unit and other functions used in previous sections. If you take the weekdays for the release dates of different versions of R for example, you could create a statement with the format function that looks like the following:

// The full weekday for the release dates is returned.

> table(format(resdates$Date,'%A'))

Friday Monday Thursday Tuesday Wednesday 2 3 1 2 3

You could also use the same method to convert dates to factors. For example, if you create a factor that is based on the release dates and separated them into years, you could use the following:

// The four digit is returned

> facdate <- factor(format(resdates$Date,'%Y'))

> facdate

[1] 2000 2000 2000 2001 2001 2002 2002 2003 2003 2004 2004

Levels: 2000 2001 2002 2003 2004 cut(thetimes,"year")

[1] 02 03 02 02 02 Levels: 02 < 03

In document RProgramming_LearnR (Page 187-192)