Learn R Programming

tind (version 0.2.4)

as.tind: Conversion to tind Class

Description

as.tind method allows for conversion of numeric and character vectors as well as objects of Date, POSIXct, and POSIXlt classes to tind objects to tind.

as.tind method for tind class allows to change the type of index of an object of tind class. Convenience functions as.year, as.quarter, as.month, as.week, as.date, as.date_time, as.time allow to quickly convert argument to the indicated type.

Usage

as.tind(x, ...)

# S3 method for numeric as.tind(x, type = NULL, tz = NULL, ...)

# S3 method for character as.tind( x, type = NULL, format = NULL, order = NULL, locale = NULL, tz = NULL, ... )

# S3 method for Date as.tind(x, ...)

# S3 method for POSIXct as.tind(x, tz = NULL, digits = 0L, ...)

# S3 method for POSIXlt as.tind(x, tz = NULL, digits = 0L, ...)

# S3 method for data.frame as.tind(x, ...)

# S3 method for tind as.tind(x, type = NULL, tz = NULL, ...)

as.year(x, ...)

as.quarter(x, ...)

as.month(x, ...)

as.week(x, ...)

as.date(x, ...)

as.date_time(x, tz = NULL, ...)

as.time(x, ...)

Value

An object of tind class of length equal to the length of the argument. For data frame version the length of the result is equal to the number of rows in the data frame.

Arguments

x

an R object (e.g., a numeric vector, a character vector, a Date or POSIXct object).

...

further arguments passed to or from other methods.

type

a character determining time index type or NULL.

tz

(optional) a character value determining the time zone (the default NULL is interpreted as the system time zone). See tzone documentation for information on time zones.

format

a character determining input format(s) as in strptind. (or NULL).

order

a character determining order(s) of time index components in the input as in parse_t (or NULL).

locale

(optional) a character value determining locale or NULL (the default, interpreted as the current system locale), see calendar-names for information on locale settings.

digits

an integer value (0--6) determining the number of decimal places for seconds to be preserved during conversion (0 by default).

Details

Numeric vectors

The following numeric representations are automatically recognised (between year 1800 and 2199): YYYY (years), YYYYQ (quarters), YYYYMM (months), and YYYYMMDD (dates). Conversion from numeric vectors to other index types requires type specification via type argument.

Date-time indices are represented as number of seconds since the Epoch (1970-01-01 00:00 UTC). Time of day is represented as the number of seconds since midnight.

Character vectors

as.tind automatically recognises many different formats. If automatic recognition fails, type argument could help identify the format. For less standard formats / representations, one can use either format argument (which is forwarded to strptind) or order argument (which is forwarded to parse_t).

Data frames

If a data frame has one column, it is converted using appropriate method depending on the column type. In case there are two or more columns, the approach depends on column types. If there are two columns representing dates and times (time of day) either as tinds or other classes recognised by the package, date-time indices are constructed using date_time function. When all columns are numeric, the method forwards to tind constructor. order argument has to be provided (via ...) indicating order of time index components in the columns. In the general case, all columns are pasted (with spaces separating them) and the resulting character vector is parsed. This will again require order argument (or type, or format). Due to pasting and subsequent parsing, this may not be computationally efficient for larger datasets.

Date and POSIXt classes

Conversion of Date objects returns time index of type "d" (date). POSIXct and POSIXlt classes are converted to index of type "t" (date-time). If time zone attribute is not set for the argument, system time zone is assumed.

Other classes representing time indices

For conversions between tind class and other classes (from packages other than base), see tind-other.

See Also

tind constructor, strptind function for format specifications, parse_t function for order specifications, tind-coercion for conversions from tind, and tind-other for conversions between tind class and other classes (from packages other than base).

Examples

Run this code
## numeric and character arguments
# years
as.tind(1999)
as.tind("1999")
# quarters
as.tind(20043)
as.tind("20043")
# months
as.tind(200109)
as.tind("2001-09")
as.tind("200109")
# need proper locale to recognise English month names
as.tind("Sep 2001", locale = "C")
# weeks (ISO 8601)
# numeric YYYYWW representation is not automatically recognised, need type
as.tind(200936, "w")
as.tind("2009-W36")
# dates
as.tind(20200726)
as.tind("2020-07-26")
# need proper locale to recognise English month names
as.tind("Jul 26, 2020", locale = "C")
as.tind("07/26/20")
# date-time
as.tind("2000-08-16 08:17:38")
# time
as.tind("08:17:38")
as.tind(08 * 3600 + 17 * 60 + 38, type = "h")

## conversion from Date and POSIXct
as.tind(Sys.Date())
as.tind(Sys.time())

## as.year, ..., as.time
# today
(x <- today())
as.year(x)
as.quarter(x)
as.month(x)
as.week(x)
# midnight
as.date_time(x)
# current time
(x <- now())
as.year(x)
as.quarter(x)
as.month(x)
as.week(x)
as.date(x)
as.time(x)

Run the code above in your browser using DataLab