compmeans: Means of a numerical vector according to a factor In descr: Descriptive Statistics

Description

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

Usage

 1 2 3 4 5 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.

Author(s)

Jakson A. Aquino [email protected], with code for weighted boxplots written by Stefan Kraft for simPopulation package.

See Also

boxplot.

Examples

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 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")) 

Example output

Mean value of "Income" according to "Sex"
Mean    N Std. Dev.
F     505.9969  900  100.8551
M     501.7278  900  100.1842
Total 503.8623 1800  100.5150
% latex table generated in R 3.4.4 by xtable 1.8-3 package
% Sun Feb 10 04:00:23 2019
\begin{table}[ht]
\centering
\begin{tabular}{lrrr}
\hline
& Mean & N & Std. Dev. \\
\hline
F & 544.31 & 1852 & 90.87 \\
M & 501.73 & 900 & 100.18 \\
Total & 530.39 & 2752 & 96.10 \\
\hline
\end{tabular}
\caption{Income according to sex}
\label{tab:incsx}
\end{table}


descr documentation built on May 2, 2019, 3:28 p.m.