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

Common grouping functions in plyr

Some of the commonly used grouping function in plyr is listed following with simple example:

  • apply - It is used to apply a function to the rows or columns of a matrix, but not generally advisable for data frames as it will coerce to a matrix first.
# Two dimensional matrix mat <- matrix(seq(1,9), 3, 3)# apply min to rows apply(mat, 1, min)
  • lapply - It is used to apply a function to each element of a list, then get a list back and is the workhorse of other ply functions underneath the code.
x <- list(a = 1, b = 1:3, c = 10:50) lapply(x, FUN = length)
  • sapply - It is used to apply a function to each element of a list in turn, but get a vector back instead of a list.
 x <- list(a = 1, b = 1:3, c = 10:50) ...

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