简体   繁体   English

双精度型和数字型有什么区别?

[英]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 .我想这与你的转换做data.frametibble Replicating your code on mtcars dataset, we get:mtcars数据集上复制您的代码,我们得到:

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 :如果我们评论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.as.numeric交换as.double不会改变任何东西。 From ?double :?double

as.double is a generic function. It is identical to as.numeric. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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