Coercion to the Date Class
as.Date.RdAlternative 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 class 'numeric'
as.Date(x, origin, ...)
# S3 method for class 'ts'
as.Date(x, offset = 0, ...)
# S3 method for class 'yearmon'
as.Date(x, frac = 0, ...)
# S3 method for class 'yearqtr'
as.Date(x, frac = 0, ...)Arguments
- x
an object to be converted.
- origin
a
Dateobject, or something which can be coerced byas.Date(origin, ...)to such an object ormissing. 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.
Value
An object of class Date.
Examples
## day zero (epoch): Jan 1, 1970
as.Date(0)
#> [1] "1970-01-01"
## conversion of July 2000 to date (beginning, mid or end of month)
ym <- yearmon(2000.5)
as.Date(ym, frac = 0)
#> [1] "2000-07-01"
as.Date(ym, frac = 0.5)
#> [1] "2000-07-16"
as.Date(ym, frac = 1)
#> [1] "2000-07-31"
## conversion of quarterly time series to dates (beginning of the months)
x <- ts(1:4, start = 2000, freq = 4)
as.Date(x)
#> [1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01"