Learn R Programming

rcompanion (version 2.5.4)

groupwiseFiveNumber: Groupwise quantiles

Description

Calculates quantiles of an ordered factor or numeric variable by one or more grouping variables

Usage

groupwiseFiveNumber(
  formula = NULL,
  data = NULL,
  var = NULL,
  group = NULL,
  type = 7,
  quantiles = c(0, 0.25, 0.5, 0.75, 1),
  digits = 3,
  ...
)

Value

A data frame of requested statistics by group.

Arguments

formula

A formula indicating the measurement variable and the grouping variables. e.g. y ~ x1 + x2.

data

The data frame to use.

var

The measurement variable to use. The name is in double quotes.

group

The grouping variable to use. The name is in double quotes. Multiple names are listed as a vector. (See example.)

type

The type of quantiles, as in the quantile function. For ordered factors, must be 1 or 3.

quantiles

A vector of quantiles to determine.

digits

For numeric variables, the number of significant figures to use in output.

...

Other arguments passed to the quantile function.

Author

Salvatore Mangiafico, [email protected]

Details

The input should include either formula and data; or data, var, and group. (See examples).

Results for ungrouped (one-sample) data can be obtained by either setting the right side of the formula to 1, e.g. y ~ 1, or by setting group=NULL when using var.

References

https://rcompanion.org/handbook/C_02.html

See Also

groupwiseMedian, groupwisePercentile

Examples

Run this code
Education = c("BA","PHD","BA","MA","HS","MA","HS","BA","BA","BA","MA","MA",
              "AA","AA","BA","BA","PHD")

Education = factor(Education, ordered=TRUE, levels=c("HS","AA","BA","MA","PHD"))

Gender = c("female","female","male","female","male","female","male","female",
           "female","female","male","male","female","male","other","female",
           "female")

County = c("Elwood","Bear Lake","Elwood","Elwood","Elwood","Bear Lake","Elwood",
           "Elwood","Elwood","Elwood","Elwood","Bear Lake","Elwood","Elwood",
           "Elwood","Elwood","Bear Lake")

Arthur = data.frame(Gender, County, Education)

Arthur$Ed.code = as.numeric(Arthur$Education)

### One-way, two-way, and one-sample for ordered factor data

groupwiseFiveNumber(Education ~ Gender, data=Arthur, type=3)

groupwiseFiveNumber(Education ~ Gender + County, data=Arthur, type=3)

groupwiseFiveNumber(Education ~ 1, data=Arthur, type=3)

### For numeric data

groupwiseFiveNumber(Ed.code ~ Gender, data=Arthur)

### Variable notation

groupwiseFiveNumber(data=Arthur, var="Education", group="Gender", type=3)

### Asking for different quantiles

groupwiseFiveNumber(Education ~ Gender, data=Arthur, type=3,
                    quantiles=c(0.00, 0.05, 0.10, 0.15, 0.20, 0.25))

### Handling missing data

Arthur2 = Arthur

Arthur2$Education[1] = NA
Arthur2$Education[2] = NA

groupwiseFiveNumber(Education ~ Gender, data=Arthur2, type=3)
                                     

Run the code above in your browser using DataLab