Skip to contents

w_range() calculates the range (maximum minus minimum) of your data. The range gives you the total spread from the smallest to the largest observed value. It provides the w_* interface for consistency with other weighted statistics, though the range itself is not affected by weights (the minimum and maximum values remain the same regardless of weighting).

Usage

w_range(data, ..., weights = NULL, na.rm = TRUE)

Arguments

data

Your survey data (a data frame or tibble)

...

The numeric variables you want to analyze. You can list multiple variables or use helpers like starts_with("income")

weights

Survey weights are accepted for interface consistency, but do not affect the range calculation. The range depends only on the observed minimum and maximum values.

na.rm

Remove missing values before calculating? (Default: TRUE). With FALSE, the result for a variable that contains missing values is NA (as in base R).

Value

The range (max - min) with sample size information, including the effective sample size (effective N) when weights are provided, and the number of valid observations used.

Details

Understanding the Results

  • Range: The difference between the largest and smallest values. A large range indicates that at least some values are far apart.

  • Effective N: Reported when weights are provided, for consistency with other weighted statistics.

  • N / Missing: Valid and missing cases. With weights, both are sums of weights (displayed rounded), as SPSS reports them under WEIGHT BY; Kish's effective N is shown by summary().

Note: The range is sensitive to outliers. A single extreme value can dramatically increase the range. Consider using w_iqr for a more robust measure of spread.

When to Use This

Use w_range() when:

  • You want a quick overview of the total spread of your data

  • You need to check for data entry errors (impossible values)

  • You want to compare the total spread across groups

Formula

\(Range = \max(x) - \min(x)\)

Note: This statistic is weight-invariant. The minimum and maximum observed values do not change when weights are applied.

References

IBM Corp. (2023). IBM SPSS Statistics 29 Algorithms. IBM Corporation.

See also

range for the base R range function.

w_iqr for the weighted interquartile range (more robust).

w_sd for weighted standard deviation (another spread measure).

describe for comprehensive descriptive statistics including range.

Other weighted_statistics: w_iqr(), w_kurtosis(), w_mean(), w_median(), w_modus(), w_quantile(), w_sd(), w_se(), w_skew(), w_var()

Examples

# Load required packages and data
library(dplyr)
data(survey_data)

# Basic range
survey_data %>% w_range(age, weights = sampling_weight)
#> 
#> Weighted Range Statistics
#> -------------------------
#> Weights: sampling_weight
#> 
#>   -------------------------------
#>   Variable   Range     N  Missing
#>   -------------------------------
#>   age       77.000  2516        0
#>   -------------------------------

# Multiple variables
survey_data %>% w_range(age, income, weights = sampling_weight)
#> 
#> Weighted Range Statistics
#> -------------------------
#> Weights: sampling_weight
#> 
#>   ---------------------------------
#>   Variable     Range     N  Missing
#>   ---------------------------------
#>   age         77.000  2516        0
#>   income    7200.000  2201      315
#>   ---------------------------------

# Grouped data
survey_data %>% group_by(region) %>% w_range(age, weights = sampling_weight)
#> 
#> Weighted Range Statistics
#> -------------------------
#> Weights: sampling_weight
#> 
#> Group: region = East
#> --------------------
#> 
#>   ------------------------------
#>   Variable   Range    N  Missing
#>   ------------------------------
#>   age       77.000  509        0
#>   ------------------------------
#> 
#> Group: region = West
#> --------------------
#> 
#>   -------------------------------
#>   Variable   Range     N  Missing
#>   -------------------------------
#>   age       77.000  2007        0
#>   -------------------------------

# In summarise context
survey_data %>% summarise(range_age = w_range(age, weights = sampling_weight))
#> # A tibble: 1 × 1
#>   range_age
#>       <dbl>
#> 1        77

# Unweighted (for comparison)
survey_data %>% w_range(age)
#> 
#> Range Statistics
#> ----------------
#> 
#>   -------------------------------
#>   Variable   Range     N  Missing
#>   -------------------------------
#>   age       77.000  2500        0
#>   -------------------------------