Learn R Programming

zoo (version 1.9-1)

as.Date: Coercion to the Date Class

Description

Alternative to the as.Date coercion function from base R which was necessary to make sure that the default origin in the as.Date numeric had a default (January 1, 1970) whereas the method in base R long had no default.

Usage

as.Date(x, ...)

# S3 method for numeric as.Date(x, origin, ...)

# S3 method for ts as.Date(x, offset = 0, ...)

# S3 method for yearmon as.Date(x, frac = 0, ...)

# S3 method for yearqtr as.Date(x, frac = 0, ...)

Value

An object of class Date.

Arguments

x

an object to be converted.

origin

a Date object, or something which can be coerced by as.Date(origin, ...) to such an object or missing. In that case, "1970-01-01" is used.

offset

numeric offset to be added to the time scale prior to coercion.

frac

numeric between 0 and 1 inclusive that indicates the fraction of the way through the period that the result represents. The default is 0 which means the beginning of the period.

...

further arguments to be passed from or to other methods.

See Also

Examples

Run this code
## day zero (epoch): Jan 1, 1970
as.Date(0)

## conversion of July 2000 to date (beginning, mid or end of month)
ym <- yearmon(2000.5)
as.Date(ym, frac = 0)
as.Date(ym, frac = 0.5)
as.Date(ym, frac = 1)

## conversion of quarterly time series to dates (beginning of the months)
x <- ts(1:4, start = 2000, freq = 4)
as.Date(x)

Run the code above in your browser using DataLab