tibble icon indicating copy to clipboard operation
tibble copied to clipboard

Tibble creation not inputting (or displaying) correct values

Open hathawayj opened this issue 2 years ago • 2 comments

The tibble creation changes the stats_time variable display to look like it goes through 2008. However, when the values are pulled it looks like they are the input values...

library(tibble)
library(dplyr)
new_t <-  seq(2006, len = 2 * 12, by = 1/12)

tibble(
    stats_time = new_t,
    month = rep(1:12, 2)
    ) |> print(n = 24)

data.frame(
    stats_time = new_t,
    month = rep(1:12, 2)
)

tibble(
    stats_time = new_t,
    month = rep(1:12, 2)
    ) |> pull(stats_time)

hathawayj avatar Jan 19 '24 22:01 hathawayj

Thanks. This is documented in https://tibble.tidyverse.org/articles/digits.html#trailing-dot . One could argue that there should always be at least one digit after the terminal dot, just to avoid this sort of confusion.

The reprex package is preferred for sharing examples, would have saved me some time to see the core of the problem. It's very easy to use and gives both the requester and the supporter an advantage:

library(tibble)

new_t <- seq(2006, len = 2 * 12, by = 1 / 12)
new_t[17:20]
#> [1] 2007.333 2007.417 2007.500 2007.583

df <- data.frame(
  stats_time = new_t,
  month = rep(1:12, 2)
)

df[17:20, ]
#>    stats_time month
#> 17   2007.333     5
#> 18   2007.417     6
#> 19   2007.500     7
#> 20   2007.583     8
as_tibble(df)[17:20, ]
#> # A tibble: 4 × 2
#>   stats_time month
#>        <dbl> <int>
#> 1      2007.     5
#> 2      2007.     6
#> 3      2008.     7
#> 4      2008.     8

Created on 2024-01-20 with reprex v2.0.2

krlmlr avatar Jan 20 '24 07:01 krlmlr

Thanks. I worry about rounding it to a different number. I could see clipping it. Sorry about the reprex(). I appreciate that your responded without it.

hathawayj avatar Jan 20 '24 16:01 hathawayj