0

I'd like to store quarterly data using a number representation where the left side represents the year and the right the quarter.

This is my code

library(tidyverse)
library(fpp)

  ausbeer %>% 
    as_tibble %>% 
    select(megalitres = x) %>% 
    mutate(year = as.double(seq(1956, 2009, 0.25)[1:211]))

For some reason, it will only show the year as integers, and it won't show the decimals.

I've checked and it's the right data underneath but I'm having a hard time making it show up.

I don't want to code them as characters because that will make visualization more difficult

Cauder
  • 2,157
  • 4
  • 30
  • 69
  • Not sure this is what you are looking for, but adding `%>% print.data.frame` would show the decimals. There are probably also other ways of making the tibble display the decimals in this case. – AndreasM Oct 23 '20 at 13:45
  • Does [this](https://stackoverflow.com/questions/51621518/how-to-make-tibbles-display-significant-digits) solve your problem? – ekoam Oct 23 '20 at 13:46
  • Does this answer your question? [Why does as\_tibble() round floats to the nearest integer?](https://stackoverflow.com/questions/48668064/why-does-as-tibble-round-floats-to-the-nearest-integer) – camille Oct 23 '20 at 13:50

1 Answers1

1

I guess this has to do with converting your data.frame into a tibble. Replicating your code on mtcars dataset, we get:

mtcars %>%
  as_tibble() %>%
  mutate(year = as.double(seq(1956, 2009, 0.25)[1:nrow(mtcars)])) %>%
  dplyr::select(year) %>%
  head

# year
# <dbl>
# 1 1956 
# 2 1956.
# 3 1956.
# 4 1957.
# 5 1957 
# 6 1957.

Here's the difference if we comment as_tibble:

# year
# 1 1956.00
# 2 1956.25
# 3 1956.50
# 4 1956.75
# 5 1957.00
# 6 1957.25

Swapping as.double with as.numeric does not change anything. From ?double:

as.double is a generic function. It is identical to as.numeric. 
AlexB
  • 3,061
  • 2
  • 17
  • 19