O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

In step 1, the mean function is applied to the auto$mpg vector, grouped according to the auto$cylinders vector. The grouping factor should be of the same length as the input vector, so that each element of the first vector can be associated with a group.

The tapply function creates groups of the first argument based on each element's group affiliation as defined by the second argument, and passes each group to the user-specified function.

Step 2 shows that we can actually group by several factors specified as a list. In this case, tapply applies the function to each unique combination of the specified factors.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required