Reputation: 47
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"
Upvotes: 1
Views: 1160
Reputation: 2361
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.
Upvotes: 5