Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Timestamps R gives us a variety of tools for working with timestamp information. Let's start off by exploring the Date object: Dates You can use

Timestamps
R gives us a variety of tools for working with timestamp information. Let's start off by exploring the Date object:
Dates
You can use the as.Date() function to convert a character string to a Date object, which will allow it to contain more time information. The string will need to be in a standard time format. We can ask for today's date by asking the system (Sys.) for the Date:
Sys.Date()
[1]"2016-05-12"
# Set as a variable
today <- Sys.Date()
today
[1]"2016-05-12"
You can also convert character strings in R to a Date object using as.Date(). You'll need to make sure its in the correct format, or use % symbols that correlate with your given format:
Code Value
%d Day of the month (decimal number)
%m Month (decimal number)
%b Month (abbreviated)
%B Month (full name)
%y Year (2 digit)
%Y Year (4 digit)
Let's show some examples of how to use as.Date and the format argument for this:
# YYYY-MM-DD
as.Date('1990-11-03')
[1]"1990-11-03"
# Using Format
as.Date("Nov-03-90",format="%b-%d-%y")
[1]"1990-11-03"
# Using Format
as.Date("November-03-1990",format="%B-%d-%Y")
[1]"1990-11-03"
Now we see how we can use as.Date() in combination with the format argument to basically convert any string representation of dates to an actual Date object, which is useful for time series analysis.
Time
Just like with Dates, we can also convert strings and work with them for time information. R uses a POSIXct object type to store time information. It's a bit outside the scope of this course, but POSIX represents a portable operating system interface, primarily for UNIX systems, but available on other operating systems as well. As far as what we need to know now, we can just use as.POSIXct() for converting string to a POSIXct object type for time series analysis, the format symbols are best seen through the help documentation for the strptime() function:
help(strptime)
strptime {base} R Documentation
Date-time Conversion Functions to and from Character
Description
Functions to convert between character representations and objects of classes "POSIXlt" and "POSIXct" representing calendar dates and times.
Usage
## S3 method for class 'POSIXct'
format(x, format ="", tz ="", usetz = FALSE, ...)
## S3 method for class 'POSIXlt'
format(x, format ="", usetz = FALSE, ...)
## S3 method for class 'POSIXt'
as.character(x,...)
strftime(x, format ="", tz ="", usetz = FALSE, ...)
strptime(x, format, tz ="")
Arguments
x
An object to be converted: a character vector for strptime, an object which can be converted to "POSIXlt" for strftime.
tz
A character string specifying the time zone to be used for the conversion. System-specific (see as.POSIXlt), but "" is the current time zone, and "GMT" is UTC. Invalid values are most commonly treated as UTC, on some platforms with a warning.
format
A character string. The default for the format methods is "%Y-%m-%d %H:%M:%S" if any element has a time component which is not midnight, and "%Y-%m-%d" otherwise. If options("digits.secs") is set, up to the specified number of digits will be printed for seconds.
...
Further arguments to be passed from or to other methods.
usetz
logical. Should the time zone abbreviation be appended to the output? This is used in printing times, and more reliable than using "%Z".
Details
The format and as.character methods and strftime convert objects from the classes "POSIXlt" and "POSIXct" to character vectors.
strptime converts character vectors to class "POSIXlt": its input x is first converted by as.character. Each input string is processed as far as necessary for the format specified: any trailing characters are ignored.
strftime is a wrapper for format.POSIXlt, and it and format.POSIXct first convert to class "POSIXlt" by calling as.POSIXlt (so they also work for class "Date"). Note that only that conversion depends on the time zone.
The usual vector re-cycling rules are applied to x and format so the answer will be of length of the longer of these vectors.
Locale-specific conversions to and from character strings are used where appropriate and available. This affects the names of the days and months, the AM/PM indicator (if used) and the separators in formats such as %x and %X, via the setting of the LC_TIME locale category. The current locale of the descriptions might mean the locale in use at the start of the R session or when these functions are first used.
The details of the formats are platform-specific, but the following are likely to be widely available: most are defined by the POSIX standard. A conversion specification is introduced by %, usually followed by a single letter or O or E and then a single letter. Any character in the format string not part of a conversion specification is interpreted literally (and %% gives %). Widely implemented conversion specifications include
%a
Abbreviated weekday name in the current locale on this platform. (Also matches full name on input: in some locales there are no abbreviations of na

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions