descr (version 1.1.4)

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, missing.include = FALSE,
          plot = getOption("descr.plot"),
          relative.widths = TRUE, col = "lightgray",
          warn = getOption("descr.warn"), …)

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.

missing.include

If TRUE, then NA values, if present in f, are included as level "NA". You can change the new level label by setting the value of descr.na.replacement option. Example: options(descr.na.replacement = "Missing").

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.

warn

Warn if conversion from factor into numeric or from numeric into factor was performed and if missing values were dropped (default: TRUE).

Further arguments to be passed to either boxplot (if w is missing) or bxp (for w weighted 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
# NOT RUN {
sex <- factor(c(rep("F", 900), rep("M", 900)))
income <- 100 * (rnorm(1800) + 5)
weight <- rep(1, 1800)
weight[sex == "F" & income > 500] <- 3
attr(income, "label") <- "Income"
attr(sex, "label") <- "Sex"
compmeans(income, sex, col = "lightgray", ylab = "income", xlab = "sex")
comp <- compmeans(income, sex, weight, plot = FALSE)
plot(comp, col = c("pink", "lightblue"), ylab = "income", xlab = "sex")

library(xtable)
# If the decimal separator in your country is a comma:
# options(OutDec = ",")
print(xtable(comp, caption = "Income according to sex", label = "tab:incsx"))
# }

Run the code above in your browser using DataLab