r - 为什么 as_tibble() 将 float 四舍五入到最接近的整数?

标签 r dplyr tibble

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

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

为什么会发生这种舍入以及如何避免它?

最佳答案

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

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

如果你想看到更多的数字,你必须打印一个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

(是的,最后两行没用)

关于r - 为什么 as_tibble() 将 float 四舍五入到最接近的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48668064/

相关文章:

r - 使用 R Shiny 中的操作按钮将行从一个 DT 移动到其他 DT

r - 在 dplyr 的过滤器中同时使用空过滤器和字符串过滤器

r - mlogit.data() 错误 : Assigned data `ids` must be compatible with existing data

r - 哪些 tidyverse 函数返回小标题?

r - 使用每个小标题中的名称从小标题列表中选择变量

r - 使用roxygen2时如何指定以哪种顺序加载S4方法

r - 是否可以在 RStudio 中检索控制台输出历史记录?

r - 用 R 中的连续索引向量化向量和矩阵的叉积

r - 使用 ftransform 和来自折叠 R 包的 fgroup_by

r - 用成对的行旋转更宽