简体   繁体   English

为什么as_tibble()round浮动到最接近的整数?

[英]Why does as_tibble() round floats to the nearest integer?

When using as_tibble in dplyr 0.7.4 and R 3.4.1 I get the following outputs 在dplyr 0.7.4和R 3.4.1中使用as_tibble时,我得到以下输出

mtcars %>% aggregate(disp ~ cyl, data=., mean) %>% as_tibble()

which outputs 哪个输出

# A tibble: 3 x 2
    cyl  disp
  <dbl> <dbl>
1  4.00   105
2  6.00   183
3  8.00   353

while

mtcars %>% aggregate(disp ~ cyl, data=., mean)

outputs 输出

  cyl     disp
1   4 105.1364
2   6 183.3143
3   8 353.1000

Not really surprisingly, the following 以下并不奇怪

mtcars %>% group_by(cyl) %>% summarise(disp=mean(disp))

gives again 又给了

# A tibble: 3 x 2
    cyl  disp
  <dbl> <dbl>
1  4.00   105
2  6.00   183
3  8.00   353

Why is this rounding happening and how can I avoid it? 为什么这种四舍五入发生了,我该如何避免呢?

This is not a rounding, it's only a way for {tibble} to display data in a pretty way: 这不是一个舍入,它只是{tibble}以一种漂亮的方式显示数据的一种方式:

> mtcars %>% 
+   aggregate(disp ~ cyl, data=., mean) %>% 
+   as_tibble() %>% 
+   pull(disp)
[1] 105.1364 183.3143 353.1000

If you want to see more digits, you have to print a data.frame: 如果要查看更多数字,则必须打印data.frame:

> mtcars %>% 
+   aggregate(disp ~ cyl, data=., mean) %>% 
+   as_tibble() %>% 
+   as.data.frame()
  cyl     disp
1   4 105.1364
2   6 183.3143
3   8 353.1000

(and yes, the two last lines are useless) (是的,最后两行没用)

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

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