This is one way:
DateTime.lt <- as.POSIXlt(DateTime)
DateTime.lt$hour <- 20
DateTime <- as.POSIXct(DateTime.lt)
Unlike POSIXct
, which stores datetimes as a single number (seconds since the epoch), POSIXlt
stores them as tuples of seconds, minutes, hours, etc., which can be accessed directly (see ?POSIXlt
for details). POSIXct
has a smaller memory footprint, and is therefore the preferred datatype for datetime objects, but for tasks such as this, temporarily converting to POSIXlt
is simple and effective.