gapply: Apply a Function by Groups

Description Usage Arguments Value Author(s) References See Also Examples

Description

Applies the function to the distinct sets of rows of the data frame defined by groups.

Usage

1
gapply(object, which, FUN, form, level, groups, ...)

Arguments

object

an object to which the function will be applied - usually a groupedData object or a data.frame. Must inherit from class "data.frame".

which

an optional character or positive integer vector specifying which columns of object should be used with FUN. Defaults to all columns in object.

FUN

function to apply to the distinct sets of rows of the data frame object defined by the values of groups.

form

an optional one-sided formula that defines the groups. When this formula is given the right-hand side is evaluated in object, converted to a factor if necessary, and the unique levels are used to define the groups. Defaults to formula(object).

level

an optional positive integer giving the level of grouping to be used in an object with multiple nested grouping levels. Defaults to the highest or innermost level of grouping.

groups

an optional factor that will be used to split the rows into groups. Defaults to getGroups(object, form, level).

...

optional additional arguments to the summary function FUN. Often it is helpful to specify na.rm = TRUE.

Value

Returns a data frame with as many rows as there are levels in the groups argument.

Author(s)

Jos<c3><a9> Pinheiro and Douglas Bates [email protected]

References

Pinheiro, J.C., and Bates, D.M. (2000) "Mixed-Effects Models in S and S-PLUS", Springer, esp. sec. 3.4.

See Also

gsummary

Examples

1
2
3
4
5
6
7
## Find number of non-missing "conc" observations for each Subject
gapply( Phenobarb, FUN = function(x) sum(!is.na(x$conc)) )

# Pinheiro and Bates, p. 127 
table( gapply(Quinidine, "conc", function(x) sum(!is.na(x))) )
changeRecords <- gapply( Quinidine, FUN = function(frm)
    any(is.na(frm[["conc"]]) & is.na(frm[["dose"]])) )

Example output

42 28 30 56 46  5 55 32 43 29  3 11 45 22 40 47 31 14  7 41 33 44 23 57 34 18 
 2  1  2  1  1  3  1  3  1  1  3  1  3  2  2  1  1  4  3  3  2  3  3  2  2  4 
36 21 15 38 10  1 58 51  4  6 19  2 27 53 37 20 12  9 17 26  8 49 39 54 25 35 
 3  3  2  4  3  2  3  3  3  3  3  3  2  2  2  3  3  4  3  2  3  3  3  4  6  2 
52 13 16 59 48 24 50 
 3  2  3  3  5  3  4 

 1  2  3  4  5  6  7 10 11 
46 33 31  9  3  8  2  1  3 

nlme documentation built on May 30, 2017, 3:12 a.m.

Related to gapply in nlme...