dplyr to output class data.frame

前端 未结 2 918
时光说笑
时光说笑 2021-02-07 09:21

I can summarise a data frame with dplyr like this:

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

To convert the output b

相关标签:
2条回答
  • 2021-02-07 09:55

    In addition to what G. Grothendieck mentioned above, you can convert it into a new dataframe:

    new_summary <- mtcars %>%
       group_by(cyl) %>%
       summarise(mean(mpg)) %>%
       as.data.frame()
    
    0 讨论(0)
  • 2021-02-07 09:59

    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)) %>%
       ungroup %>%
       as.data.frame()
    

    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)
    

    (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 it's also too low for you.)

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

    0 讨论(0)
提交回复
热议问题