Learn R Programming

sjstats (version 0.17.4)

grpmean: Summary of mean values by group

Description

Computes mean, sd and se for each sub-group (indicated by grp) of dv.

Usage

grpmean(x, dv, grp, weights = NULL, digits = 2, out = c("txt",
  "viewer", "browser"), encoding = "UTF-8", file = NULL)

Arguments

x

A (grouped) data frame.

dv

Name of the dependent variable, for which the mean value, grouped by grp, is computed.

grp

Factor with the cross-classifying variable, where dv is grouped into the categories represented by grp. Numeric vectors are coerced to factors.

weights

Name of variable in x that indicated the vector of weights that will be applied to weight all observations. Default is NULL, so no weights are used.

digits

Numeric, amount of digits after decimal point when rounding estimates and values.

out

Character vector, indicating whether the results should be printed to console (out = "txt") or as HTML-table in the viewer-pane (out = "viewer") or browser (out = "browser"), of if the results should be plotted (out = "plot", only applies to certain functions). May be abbreviated.

encoding

Character vector, indicating the charset encoding used for variable and value labels. Default is "UTF-8". Only used when out is not "txt".

file

Destination file, if the output should be saved as file. Only used when out is not "txt".

Value

For non-grouped data frames, grpmean() returns a data frame with following columns: term, mean, N, std.dev, std.error and p.value. For grouped data frames, returns a list of such data frames.

Details

This function performs a One-Way-Anova with dv as dependent and grp as independent variable, by calling lm(count ~ as.factor(grp)). Then contrast is called to get p-values for each sub-group. P-values indicate whether each group-mean is significantly different from the total mean.

Examples

Run this code
# NOT RUN {
data(efc)
grpmean(efc, c12hour, e42dep)

data(iris)
grpmean(iris, Sepal.Width, Species)

# also works for grouped data frames
library(dplyr)
efc %>%
  group_by(c172code) %>%
  grpmean(c12hour, e42dep)

# weighting
efc$weight <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5))
grpmean(efc, c12hour, e42dep, weights = weight)

# }

Run the code above in your browser using DataLab