Skip to contents

Calculates the mean across multiple variables for each row. This is the standard way to create scale scores from survey items — the R equivalent of SPSS's COMPUTE score = MEAN(var1, var2, var3).

Use min_valid to require a minimum number of non-missing items, mirroring SPSS's MEAN.n() syntax: min_valid = 2 corresponds to MEAN.2(a, b, c).

Usage

row_means(data, ..., min_valid = NULL, na.rm = TRUE)

Arguments

data

Your survey data (a data frame or tibble). Inside mutate(), the recommended form is pick(): mutate(score = row_means(pick(item1, item2, item3))). It also works in a grouped mutate(); . (the magrittr placeholder) is the whole data set and therefore only works without groups.

...

The variables to average. Use bare column names separated by commas, or tidyselect helpers like starts_with("trust"). If no variables are specified, all numeric columns in data are used (useful with pick(); non-numeric columns are ignored with a warning).

min_valid

Minimum number of non-missing values required to compute a mean (a whole number). If a row has fewer valid values, NA is returned. Default is NULL (compute mean if at least 1 value is valid).

na.rm

Remove missing values before calculating? Default: TRUE.

Value

A numeric vector with one value per row — the mean across the selected variables. Use inside dplyr::mutate() to add it as a new column.

Details

How It Works

For each row, row_means() computes the arithmetic mean of the selected variables. Missing values are ignored by default, so a respondent who answered 2 out of 3 items still gets a score.

The min_valid Parameter

In practice, you often want to require a minimum number of valid responses. If someone only answered 1 out of 5 items, a mean based on a single item may not be reliable. Set min_valid to control this:

  • min_valid = NULL (default): Compute mean with any number of valid values (at least 1)

  • min_valid = 2: Require at least 2 valid values

  • min_valid = 3: Require at least 3 valid values

When to Use This

Use row_means() after checking reliability with reliability:

  1. Run reliability() to check if items form a reliable scale

  2. If Cronbach's Alpha is acceptable (typically > .70), create the index

  3. Use the index in further analyses (t-tests, correlations, regression)

See also

row_sums() for row-wise sums, row_count() for counting specific values, pomps() for rescaling to 0-100

Other scale: efa(), pomps(), reliability(), row_count(), row_sums()

Examples

library(dplyr)
data(survey_data)

# Create a trust scale from 3 items (recommended: pick())
survey_data <- survey_data %>%
  mutate(m_trust = row_means(pick(trust_government, trust_media,
                                  trust_science)))

# tidyselect helpers inside pick(); also works after group_by()
survey_data <- survey_data %>%
  group_by(region) %>%
  mutate(m_trust = row_means(pick(starts_with("trust")))) %>%
  ungroup()

# Alternative without groups: the data placeholder `.`
survey_data <- survey_data %>%
  mutate(m_trust = row_means(., trust_government, trust_media, trust_science))

# Require at least 2 valid items (like SPSS MEAN.2)
survey_data <- survey_data %>%
  mutate(m_trust = row_means(., trust_government, trust_media,
                             trust_science, min_valid = 2))