posixt-setters | R Documentation |
These are POSIXct/POSIXlt methods for the setter generics.
set_year()
sets the year.
set_month()
sets the month of the year. Valid values are in the range
of [1, 12]
.
set_day()
sets the day of the month. Valid values are in the range
of [1, 31]
.
There are sub-daily setters for setting more precise components, up to a precision of seconds.
## S3 method for class 'POSIXt'
set_year(x, value, ..., invalid = NULL, nonexistent = NULL, ambiguous = x)
## S3 method for class 'POSIXt'
set_month(x, value, ..., invalid = NULL, nonexistent = NULL, ambiguous = x)
## S3 method for class 'POSIXt'
set_day(x, value, ..., invalid = NULL, nonexistent = NULL, ambiguous = x)
## S3 method for class 'POSIXt'
set_hour(x, value, ..., invalid = NULL, nonexistent = NULL, ambiguous = x)
## S3 method for class 'POSIXt'
set_minute(x, value, ..., invalid = NULL, nonexistent = NULL, ambiguous = x)
## S3 method for class 'POSIXt'
set_second(x, value, ..., invalid = NULL, nonexistent = NULL, ambiguous = x)
x |
A date-time vector. |
value |
The value to set the component to. For |
... |
These dots are for future extensions and must be empty. |
invalid |
One of the following invalid date resolution strategies:
Using either If If |
nonexistent |
One of the following nonexistent time resolution strategies, allowed to be either length 1, or the same length as the input:
Using either If If |
ambiguous |
One of the following ambiguous time resolution strategies, allowed to be either length 1, or the same length as the input:
Alternatively, Finally, If If |
x
with the component set.
x <- as.POSIXct("2019-02-01", tz = "America/New_York")
# Set the day
set_day(x, 12:14)
# Set to the "last" day of the month
set_day(x, "last")
# You cannot set a date-time to an invalid date like you can with
# a year-month-day. Instead, the default strategy is to error.
try(set_day(x, 31))
set_day(as_year_month_day(x), 31)
# You can resolve these issues while setting the day by specifying
# an invalid date resolution strategy with `invalid`
set_day(x, 31, invalid = "previous")
y <- as.POSIXct("2020-03-08 01:30:00", tz = "America/New_York")
# Nonexistent and ambiguous times must be resolved immediately when
# working with R's native date-time types. An error is thrown by default.
try(set_hour(y, 2))
set_hour(y, 2, nonexistent = "roll-forward")
set_hour(y, 2, nonexistent = "roll-backward")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.