繁体   English   中英

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

[英]What's the difference between a double and a numeric?

我想使用数字表示存储季度数据,其中左侧代表年份,右侧代表季度。

这是我的代码

library(tidyverse)
library(fpp)

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

出于某种原因,它只会将年份显示为整数,而不会显示小数。

我已经检查过,下面是正确的数据,但我很难让它显示出来。

我不想将它们编码为字符,因为这会使可视化更加困难

我想这与你的转换做data.frametibble 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.

如果我们评论as_tibble则有以下不同:

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

as.numeric交换as.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