Reuver, B de (epi)
2024-Oct-23 08:11 UTC
[R] Outputting a difftime object in PASTE or AS.CHARACTER is missing the unit + expand the format options?
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 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" 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(). If someone really need just the numeric value without the unit, you can already use as.numeric(dur_time). 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 regards, Bas de reuver (UMCG, The Netherlands) ________________________________ De inhoud van dit bericht is vertrouwelijk en alleen bestemd voor de geadresseerde(n). Anderen dan de geadresseerde(n) mogen geen gebruik maken van dit bericht, het niet openbaar maken of op enige wijze verspreiden of vermenigvuldigen. Het UMCG kan niet aansprakelijk gesteld worden voor een incomplete aankomst of vertraging van dit verzonden bericht. The contents of this message are confidential and only intended for the eyes of the addressee(s). Others than the addressee(s) are not allowed to use this message, to make it public or to distribute or multiply this message in any way. The UMCG cannot be held responsible for incomplete reception or delay of this transferred message.
Rui Barradas
2024-Oct-24 11:18 UTC
[R] Outputting a difftime object in PASTE or AS.CHARACTER is missing the unit + expand the format options?
?s 09:11 de 23/10/2024, Reuver, B de (epi) via R-help escreveu:> 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 > > 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" > > 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(). > If someone really need just the numeric value without the unit, you can already use as.numeric(dur_time). > 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 > > regards, > Bas de reuver (UMCG, The Netherlands) > ________________________________ > De inhoud van dit bericht is vertrouwelijk en alleen bestemd voor de geadresseerde(n). Anderen dan de geadresseerde(n) mogen geen gebruik maken van dit bericht, het niet openbaar maken of op enige wijze verspreiden of vermenigvuldigen. Het UMCG kan niet aansprakelijk gesteld worden voor een incomplete aankomst of vertraging van dit verzonden bericht. > > The contents of this message are confidential and only intended for the eyes of the addressee(s). Others than the addressee(s) are not allowed to use this message, to make it public or to distribute or multiply this message in any way. The UMCG cannot be held responsible for incomplete reception or delay of this transferred message. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, There are, in base R, methods as.list.difftime as.double.difftime but not as.character.difftime. If you write your own, it will be a functionality that only exists in your system and this can sometimes be a source for unexpected behavior. as.character.difftime <- function(x) { u <- units(x) paste(unclass(x), u) } start_time <- Sys.time() result <- sum(1:10000) dur_time <- Sys.time() - start_time as.character(dur_time) #> [1] "0.000754117965698242 secs" Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
Martin Maechler
2024-Oct-24 15:13 UTC
[R] Outputting a difftime object in PASTE or AS.CHARACTER is missing the unit + expand the format options?
>>>>> 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"