In R, how to prevent the by()
function from changing POSIXct to numeric values automatically?
In the following example, the time
object is class "POSIXct" "POSIXt"
. When using by()
function, the output is numeric
. But when doing what by()
does manually, e.g. min(time[index=='a'])
, the output is still "POSIXct" "POSIXt"
, which is desired. Is there a way to prevent the by()
function from changing POSIXct to numeric values automatically?
time = c("0001-03-04 05:36:00 UTC", "0001-03-04 05:50:00 UTC", "0001-03-04 06:05:00 UTC")
time = as.POSIXct(time, tz = "UTC")
index = c("a", "a", "b")
by(time, index, min) # results are numeric values.
min(time[index=='a']) # "0001-03-04 05:36:00 UTC"