These functions have been written as equivalents of SPSS' MEAN.x and SUM.x functions, which only compute means and sums if enough cases have valid values.

validMeans(...,
           requiredValidValues = 0,
           returnIfInvalid = NA,
           silent = FALSE)
validSums(...,
          requiredValidValues = 0,
          returnIfInvalid = NA,
          silent = FALSE)

Arguments

...

Either a dataframe or vectors for which to compute the mean or sum.

requiredValidValues

How many values must be valid (i.e. nonmissing) to compute the mean or sum. If a number lower than 1 is provided, it is interpreted as proportion, and the number of variables is computed. For example, if requiredValidValues=.8, 80% of the variables must have valid values. If 'all' is specified, all values must be valid (in which case the functions are equal to rowMeans and rowSums).

returnIfInvalid

Wat to return for cases that don't have enough valid values.

silent

Whether to show the number of cases that have to be valid if requiredValidValues is a proportion.

Value

A numeric vector with the resulting means or sums.

See also

rowMeans, rowSums

Examples

validMeans(mtcars$cyl, mtcars$disp);
#> Argument 'requiredValidValues' was set to a proportion (0), so only computing a mean for cases where that proportion of variables (i.e. 0%, or 0 variables) have valid values.
#> [1] 83.00 83.00 56.00 132.00 184.00 115.50 184.00 75.35 72.40 86.80 #> [11] 86.80 141.90 141.90 141.90 240.00 234.00 224.00 41.35 39.85 37.55 #> [21] 62.05 163.00 156.00 179.00 204.00 41.50 62.15 49.55 179.50 75.50 #> [31] 154.50 62.50
validSums(mtcars$cyl, mtcars$disp, requiredValidValues = .8);
#> Argument 'requiredValidValues' was set to a proportion (0.8), so only computing a mean for cases where that proportion of variables (i.e. 80%, or 2 variables) have valid values.
#> [1] 166.0 166.0 112.0 264.0 368.0 231.0 368.0 150.7 144.8 173.6 173.6 283.8 #> [13] 283.8 283.8 480.0 468.0 448.0 82.7 79.7 75.1 124.1 326.0 312.0 358.0 #> [25] 408.0 83.0 124.3 99.1 359.0 151.0 309.0 125.0
### Or specifying a dataframe validSums(mtcars);
#> Argument 'requiredValidValues' was set to a proportion (0), so only computing a mean for cases where that proportion of variables (i.e. 0%, or 0 variables) have valid values.
#> Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive #> 328.980 329.795 259.580 426.135 #> Hornet Sportabout Valiant Duster 360 Merc 240D #> 590.310 385.540 656.920 270.980 #> Merc 230 Merc 280 Merc 280C Merc 450SE #> 299.570 350.460 349.660 510.740 #> Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental #> 511.500 509.850 728.560 726.644 #> Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla #> 725.695 213.850 195.165 206.955 #> Toyota Corona Dodge Challenger AMC Javelin Camaro Z28 #> 273.775 519.650 506.085 646.280 #> Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa #> 631.175 208.215 272.570 273.683 #> Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E #> 670.690 379.590 694.710 288.890