1
votes

I've recently started utilizing the dplyr package for data manipulation but don't have a complete grasp on the output of group_by(). In the example below how can my single iris.2 object have 4 different classes? How does the function know which class to use? IE if I call print it will call the print.tbl_df method and not the print.data.frame.

library(dplyr)
iris.1 <- iris
iris.2 <- group_by(iris, Species)
class(iris.1)

[1] "data.frame"

class(iris.2)

[1] "grouped_df" "tbl_df" "tbl" "data.frame"

1
You might want to skim adv-r.had.co.nz/OO-essentials.html#s3hadley

1 Answers

4
votes

The class function returns the complete inheritance of the object. In your example, iris.2 is first an object of the class grouped_df. Any methods for the grouped_df class will take precedent. Next, it is a tbl_df class, then a tbl class and finally a data.frame. When calling a method such as print, R will look for the print method of these classes in that order and will use the first it encounters.