Learn R Programming

plotrix (version 3.8-2)

brkdnNest: Perform a nested breakdown of numeric values

Description

Breaks down a numeric or categorical element of a data frame by one or more categorical elements.

Usage

brkdnNest(formula,data,FUN=c("mean","sd","sd","valid.n"),label1="Overall",
  trueval=TRUE)

Value

A list with as many elements as there are functions in FUN. It is probably best to always specify four functions (summary measure, upper dispersion measure, lower dispersion measure and number of valid observations) even if this is redundant as in the default.

This function is similar to brkdn in the prettyR package, but is structured to be used with the barNest function. It produces one or more measures for the overall data, then the subsets of the data defined by the first variable to the right of the tilde, then the subsets defined by the first and second variable, and so on.

Arguments

formula

A formula with a numeric element of a data frame on the left and one or more categorical elements on the right.

data

A data frame containing the elements in formula.

FUN

The functions to be applied to successive breakdowns.

label1

The label to use for the overall value of the first function.

trueval

The value to use in calculating proportions or sums of a categorical response variable. See Details.

Author

Jim Lemon

Details

brkdnNest performs a nested breakdown of an element of a data frame by one or more categorical elements. For each category and optional subcategories, the variable on the left of the formula is summarized as specified by the functions named in FUN.

If trueval is not NA, brkdnNest will calculate the proportion of trueval values in the response variable out of the total valid responses. If the function valid.n is the first function in FUN, the counts of the groups and subgroups will be returned.

Two specialized summary functions are defined within brkdnNest. sumbrk returns the count of values in a factor equal to trueval, and propbrk returns the proportion of values equal to trueval. Be aware that if a categorical variable is specified on the left of the formula, functions which expect numeric data such as mean should not be included in FUN.

The user should take care when specifying different summary functions. barNest expects a summary measure as the first component of the list and measures of dispersion as the second and third. If two different measures of dispersion are passed, the first must calculate the upper and the second the lower measure.

See Also

Examples

Run this code
 brkdntest<-data.frame(Age=rnorm(100,25,10),
  Sex=factor(sample(c("M","F"),100,TRUE)),
  Marital=sample(c("M","X","S","W"),100,TRUE),
  Employ=sample(c("FT","PT","NO"),100,TRUE))
 brkdnNest(formula=Age~Sex+Marital+Employ,data=brkdntest)
 # show the proportion of unemployed with binomial confidence intervals
 brkdnNest(formula=Employ~Sex+Marital,data=brkdntest,
  FUN=c("propbrk","binciWu","binciWl"),trueval="NO")

Run the code above in your browser using DataLab