[R] Outputting a difftime object in PASTE or AS.CHARACTER is missing the unit + expand the format options?

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Thu Oct 24 17:13:20 CEST 2024


>>>>> Reuver, B de \(epi\) via R-help 
>>>>>     on Wed, 23 Oct 2024 08:11:50 +0000 writes:

    > Hello,
    > I was working on an R script using the datediff object, to log certain durations in the data processing.
    > I ran in to the issue that outputing a datediff object using PASTE will leave out the time unit by default.

    > When you paste() or as.character() a datediff object it only outputs the numeric value, for example "1.23", so without the time unit secs, minutes, hours etc. This is arguably is not very useful, because 1.23 seconds is very different from 1.23 hours.

    > ```
    > start_time <- Sys.time()
    > result <- sum(1:10000)
    > dur_time <- Sys.time() - start_time

Note that  print(  ... )   below is unneeded:
 in R's toplevel, "auto" print() is (almost) always called

    > print(paste(Sys.time(), "the process duration:", dur_time)) # "2024-10-23 09:57:10.69578 the process duration: 0.00280904769897461"

    > print(dur_time)               # "Time difference of 0.002809048 secs"
    > print(paste(dur_time))        # "0.00280904769897461"
    > print(as.character(dur_time)) # "0.00280904769897461"
    > print(format(dur_time))       # "0.002809048 secs"

i.e.,  the format()  method is doing the right thing, as it should.

The as.character() method shouldn't (and will not) be changed here.


    > print(paste(dur_time, attr(dur_time, "units"))) # "0.00280904769897461 secs"
    > ```

    > So my questions are:

    > 1)  is it possible to have the default output for as.character for the datediff object also include the time unit? That way, it automatically outputs for example "1.23 seconds" when used in combination with paste().

No, see above.  You should use format() in such situations, not
just for "difftime" objects.

    > If someone really need just the numeric value without the unit, you can already use as.numeric(dur_time).

indeed.
But as  as.character()  is defined (typically / in base R) to get rid of attributes.

    > 2) Is it possible to expand the format() options for the datediff object, so that it also accepts a strptime-like string, so like %H:%M:%S ? That way you have more output options without having to rely on third-party libraries

That would be possible, but I'm not at all sure that it's
desirable / necessary  .. (but opinions may differ, I may change
mine after some good discussion...).

Did you look at the  ?difftime  help page  and its 'Examples' ?

I think everything you need is almost available.
The formatting in %H:%M .. etc is not directly possible,
and I think for good reasons:
Somewhat experienced R users can currently quickly see the
difference between  Date, Date-Times, and "difftime" objects
from their print output,  and I think that is desirable.


start_time <- structure(1729777, class = c("POSIXct", "POSIXt"))
result <- sum(1:10000)
nowTime <- structure(1729888.123123123, class = c("POSIXct", "POSIXt"))
##                          ^^^^^^^^^^ to see precision (not soo accurate with POSIXct!)

(dur_time <- nowTime - start_time) # -> print
## Time difference of 1.852052 mins
format(dur_time)            # --> "1.852052 mins"       -- MM: isn't that good!
format(dur_time, digits=12) # --> "1.85205205205 mins"

## You can switch from minutes to seconds :
dtime <- dur_time; units(dtime) <- "secs"
dtime ## Time difference of 111.1231 secs
format(dtime, digits = 10)
## [1] "111.1231231 secs"



More information about the R-help mailing list