为什么 as_tibble() 将浮点数四舍五入到最接近的整数? [英] Why does as_tibble() round floats to the nearest integer?

查看:16
本文介绍了为什么 as_tibble() 将浮点数四舍五入到最接近的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 dplyr 0.7.4 和 R 3.4.1 中使用 as_tibble 时,我得到以下输出

When using as_tibble in dplyr 0.7.4 and R 3.4.1 I get the following outputs

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

哪个输出

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

同时

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

输出

  cyl     disp
1   4 105.1364
2   6 183.3143
3   8 353.1000

不出意外,以下是

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

再给

# 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?

推荐答案

这不是四舍五入,它只是 {tibble} 以漂亮方式显示数据的一种方式:

This is not a rounding, it's only a way for {tibble} to display data in a pretty way:

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

如果你想看到更多的数字,你必须打印一个data.frame:

If you want to see more digits, you have to print a 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)

这篇关于为什么 as_tibble() 将浮点数四舍五入到最接近的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆