[Rd] Printing digits.secs on data.frame?

John Muschelli mu@che|||j2 @end|ng |rom gm@||@com
Thu Jul 18 18:14:27 CEST 2024


Is there a way to have printing data.frames with POSIXct to display
milliseconds if digits.secs is set as a default?

You can use the digits argument in print, such as print(df, digits = 3) to
get the intended output, but I assumed it was done with the option
digits.secs set.  Tibbles by default do this printing, which is shown
below, but I was unsure if digits.secs should affect printing data.frames,
as we see below it affects printing POSIXct outside of a data.frame.

``` r
df = structure(list(time = structure(c(1509375600, 1509375600.03333,
                                       1509375600.06667, 1509375600.1,
1509375600.13333, 1509375600.16667
), class = c("POSIXct", "POSIXt"), tzone = "GMT"),
X = c(0.188,
      0.18, 0.184, 0.184, 0.184, 0.184),
Y = c(0.145, 0.125, 0.121,
      0.121, 0.117, 0.125),
Z = c(-0.984, -0.988, -0.984, -0.992, -0.988,
      -0.988)), row.names = c(NA, 6L), class = "data.frame")
options(digits.secs = NULL)
getOption("digits.secs")
#> NULL
df
#>                  time     X     Y      Z
#> 1 2017-10-30 15:00:00 0.188 0.145 -0.984
#> 2 *2017-10-30 15:00:00* 0.180 0.125 -0.988
#> 3 2017-10-30 15:00:00 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00 0.184 0.125 -0.988
print(df)
#>                  time     X     Y      Z
#> 1 2017-10-30 15:00:00 0.188 0.145 -0.984
#> 2 *2017-10-30 15:00:00* 0.180 0.125 -0.988
#> 3 2017-10-30 15:00:00 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00 0.184 0.125 -0.988
df$time
#> [1] "2017-10-30 15:00:00 GMT" "*2017-10-30 15:00:00 GMT*"
#> [3] "2017-10-30 15:00:00 GMT" "2017-10-30 15:00:00 GMT"
#> [5] "2017-10-30 15:00:00 GMT" "2017-10-30 15:00:00 GMT"

print(df, digits = 3)
#>                      time     X     Y      Z
#> 1 2017-10-30 15:00:00.000 0.188 0.145 -0.984
#> 2 *2017-10-30 15:00:00.033* 0.180 0.125 -0.988
#> 3 2017-10-30 15:00:00.066 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00.099 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00.133 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00.166 0.184 0.125 -0.988
tibble::as_tibble(df)
#> # A tibble: 6 × 4
#>   time                    X     Y      Z
#>   <dttm>              <dbl> <dbl>  <dbl>
#> 1 2017-10-30 15:00:00 0.188 0.145 -0.984
#> 2 *2017-10-30 15:00:00* 0.18  0.125 -0.988
#> 3 2017-10-30 15:00:00 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00 0.184 0.125 -0.988
```

We see by default tibbles do this printing

``` r
options(digits.secs = 3)
getOption("digits.secs")
#> [1] 3
df
#>                  time     X     Y      Z
#> 1 2017-10-30 15:00:00 0.188 0.145 -0.984
#> 2 *2017-10-30 15:00:00* 0.180 0.125 -0.988
#> 3 2017-10-30 15:00:00 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00 0.184 0.125 -0.988
print(df)
#>                  time     X     Y      Z
#> 1 2017-10-30 15:00:00 0.188 0.145 -0.984
#> 2* 2017-10-30 15:00:00* 0.180 0.125 -0.988
#> 3 2017-10-30 15:00:00 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00 0.184 0.125 -0.988
```

We see that this affects printing POSIXct outside of a data.frame

``` r
df$time
#> [1] "2017-10-30 15:00:00.000 GMT" "*2017-10-30 15:00:00.033 GMT*"
#> [3] "2017-10-30 15:00:00.066 GMT" "2017-10-30 15:00:00.099 GMT"
#> [5] "2017-10-30 15:00:00.133 GMT" "2017-10-30 15:00:00.166 GMT"
print(df, digits = 3)
#>                      time     X     Y      Z
#> 1 2017-10-30 15:00:00.000 0.188 0.145 -0.984
#> 2 *2017-10-30 15:00:00.033* 0.180 0.125 -0.988
#> 3 2017-10-30 15:00:00.066 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00.099 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00.133 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00.166 0.184 0.125 -0.988
tibble::as_tibble(df)
#> # A tibble: 6 × 4
#>   time                        X     Y      Z
#>   <dttm>                  <dbl> <dbl>  <dbl>
#> 1 2017-10-30 15:00:00.000 0.188 0.145 -0.984
#> 2 *2017-10-30 15:00:00.033* 0.18  0.125 -0.988
#> 3 2017-10-30 15:00:00.066 0.184 0.121 -0.984
#> 4 2017-10-30 15:00:00.099 0.184 0.121 -0.992
#> 5 2017-10-30 15:00:00.133 0.184 0.117 -0.988
#> 6 2017-10-30 15:00:00.166 0.184 0.125 -0.988
```

<sup>Created on 2024-07-18 with [reprex v2.1.0](https://reprex.tidyverse.org
)</sup>

<details style="margin-bottom:10px;">
<summary>
Session info
</summary>

``` r
sessioninfo::session_info()
#> ─ Session info
───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.0 (2024-04-24)
#>  os       macOS Sonoma 14.4.1
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2024-07-18
#>  pandoc   3.2 @ /usr/local/bin/ (via rmarkdown)
#>
```

</details>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list