在 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
while
mtcars %>% aggregate(disp ~ cyl, data=., mean)
outputs
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
(是的,最后两行没用)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)