Learn R Programming

descr (version 1.0.0)

compmeans: Means of a numerical vector according to a factor

Description

Calculates the means of a numerical vector according to a factor.

Usage

compmeans(x, f, w, sort = FALSE, maxlevels = 60,
          user.missing, plot = getOption("descr.plot"),
          relative.widths = TRUE, col = "lightgray", ...)

Arguments

x
A numeric vector.
f
A factor.
w
Optional vector with weights.
sort
If TRUE, sorts the lines by the means values.
maxlevels
Maximum number of levels that x converted into factor should have.
user.missing
Character vector, indicating what levels of f must be treated as missing values.
plot
Logical: if TRUE (default), a boxplot is produced. You may put

options(descr.plot = FALSE)

in your .Rprofile to change the default function behavior.

relative.widths
If TRUE, the boxes widths will be proportional to the number of elements in each level of f.
col
Vector with the boxes colors.
...
Further arguments to be passed to boxplot.

Value

  • A matrix with class c("matrix", "meanscomp") with labels attributes for x and f. The returned object can be plotted, generating a boxplot of x grouped by f.

See Also

boxplot.

Examples

Run this code
educ <- sample(c(1, 2), 200, replace = TRUE, prob = c(0.3, 0.7))
educ <- factor(educ, levels = c(1, 2), labels = c("Low", "High"))
income <- rnorm(200, 1000, 100) + 100 * as.numeric(educ)
attr(educ, "label") <- "Education level"
attr(income, "label") <- "Income"
w <- sample(c(10, 15, 19), 200, replace = TRUE)

compmeans(income, educ, col = c("#FFD0E0", "#D8E8FF"))
comp <- compmeans(income, educ, w, plot = FALSE)
comp
plot(comp)
print(xtable(comp))
# If the decimal separator in your country is a comma:
# options(OutDec = ",")
print(xtable(comp, align = "lrrr", display = c("s", "f", "d", "f")))

Run the code above in your browser using DataLab