Skip to contents

Calculates the sum across multiple variables for each row. This is the R equivalent of SPSS's COMPUTE total = SUM(var1, var2, var3).

Use min_valid to require a minimum number of non-missing items, mirroring SPSS's SUM.n() syntax.

Usage

row_sums(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 sum across the selected variables.

Details

When to Use row_sums() vs row_means()

  • row_means(): For Likert-type scales where you want an average score (preserves the original scale range)

  • row_sums(): For count-based scores (e.g., number of symptoms endorsed) or when you need a total score

See also

row_means() for row-wise means, row_count() for counting specific values

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

Examples

library(dplyr)
data(survey_data)

# Total score across items
survey_data <- survey_data %>%
  mutate(total = row_sums(., trust_government, trust_media, trust_science))

# With min_valid (like SPSS SUM.3)
survey_data <- survey_data %>%
  mutate(total = row_sums(., trust_government, trust_media,
                          trust_science, min_valid = 3))