Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr to output class data.frame

Tags:

r

dplyr

I can summarise a data frame with dplyr like this:

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

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

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

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

like image 978
luciano Avatar asked Apr 09 '14 15:04

luciano


People also ask

Does dplyr work with data frame?

All of the dplyr functions take a data frame (or tibble) as the first argument. Rather than forcing the user to either save intermediate objects or nest functions, dplyr provides the %>% operator from magrittr.

What is the difference between Tibble and Dataframe in R?

Tibbles vs data frames There are two main differences in the usage of a data frame vs a tibble: printing, and subsetting. Tibbles have a refined print method that shows only the first 10 rows, and all the columns that fit on screen. This makes it much easier to work with large data.


2 Answers

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.

like image 141
G. Grothendieck Avatar answered Nov 15 '22 18:11

G. Grothendieck


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()
like image 37
S.Zhong Avatar answered Nov 15 '22 17:11

S.Zhong