xapply.formula: Apply a function over a ragged array and return a data frame

Description Usage Arguments Details Examples

Description

Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.

Usage

1
2
3

Arguments

formula

a two-sided formula: the lhs identifies the variable(s) that are the first argument of FUN (if there is more than one as in cbind( x, y)), then FUN is applied to each in turn. Note that this behaviour is different from that of capply, where FUN is applied to the resulting data frame. The rhs identifies the variables to be used to split the data into subsets.

data

a data frame.

FUN

a function.

...

additional arguments to the function

subset

a condition to subset the data frame.

na.action

determines whether to omit or include NAs in the grouping factors. Use na.include so NAs will form a distinct level.

Details

xapply works like aggregate except that the result is returned as a vector in a data frame with the levels of the by variable replicated according to the length of the result.

The intention in writing xapply was to facilitate the creation of 'prediction data frames' extending expand.grid so that the values of a variable can easily depend on the value of a factor. For example, predicting weight from height it might be desired to have a range of heights for each sex that is consistent with the conditional distribution.

Suppose a data frame hw contains variables Sex, height and weight. Instead of

1
2
3
4
   > fit <- lm ( weight ~ height * Sex, hw)
   > pred <- expand.grid( Sex = levels(hw$Sex), height = quantile( hw$height, c(0,5,25,50,75,95,100)/100))
   > pred$weight <- predict( fit, pred)
   > xyplot( weight ~ height, pred, groups = Sex, type = 'b')

we can have:

1
2
3
4
5
   > fit <- lm ( weight ~ height * Sex, hw)
   > pred <- expand.grid( Sex = levels(hw$Sex) )
   > pred <- merge( pred, xapply( height ~ Sex, hw, quantile, c(0,5,25,50,75,95,100)/100))
   > pred$weight <- predict( fit, pred)
   > xyplot( weight ~ height, pred, groups = Sex, type = 'b')

Examples

1
2
3
4
5
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as

gmonette/spida15 documentation built on May 17, 2019, 7:26 a.m.