dplyr输出类data.frame [英] dplyr to output class data.frame

查看:102
本文介绍了dplyr输出类data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用 dplyr 总结一个数据框,如下所示:

I can summarise a data frame with dplyr like this:

mtcars %.%
group_by(cyl) %.%
summarise(mean(mpg))

要将输出转换回课程 data.frame ,我目前的方法是:

To convert the output back to class data.frame, my current approach is this:

as.data.frame(mtcars %.%
group_by(cyl) %.%
summarise(mean(mpg)))

有没有办法得到 dplyr 输出一个类 data.frame 而不必使用 as.data.frame

Is there any way to get dplyr to output a class data.frame without having to use as.data.frame?

推荐答案

正如在评论中指出的那样,您可能不需要转换它,因为它可能足够好,它从数据框架继承。如果这还不够好,那么这仍然使用 as.data.frame ,但稍微更优雅:

As was pointed out in the comments you might not need to convert it since it might be good enough that it inherits from data frame. If that is not good enough then this still uses as.data.frame but is slightly more elegant:

mtcars %>%
   group_by(cyl) %>%
   summarise(mean(mpg)) %>%
   as.data.frame()

ADDED 我刚刚阅读评论,你想要的原因是为了避免打印输出的截断。在这种情况下,只需在 .Rprofile 文件中定义此选项:

ADDED I just read in the comments that the reason you want this is to avoid the truncation of printed output. In that case just define this option, possibly in your .Rprofile file:

options(dplyr.print_max = Inf)

(请注意,您仍然可以按最大定义与打印相关联的max.print选项,所以您也需要设置一个,如果它也太低。)

(Note that you can still hit the maximum defined by the "max.print" option associated with print so you would need to set that one too if its also too low for you.)

更新:更改%。%%>%反映dplyr的变化。

Update: Changed %.% to %>% to reflect changes in dplyr.

这篇关于dplyr输出类data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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