简体   繁体   中英

What's the difference between a double and a numeric?

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

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. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM