Learn R Programming

sjmisc (version 1.0.0)

mean_n: Compute row means with min amount of valid values

Description

This function is similar to the SPSS MEAN.n function and computes row means from a data.frame or matrix if at least n values of a row are valid (and not NA).

Usage

mean_n(dat, n)

Arguments

dat
a data.frame with at least two columns, where row means are applied.
n
the amount of valid values per row to calculate the row mean. If a row's amount of valid values is less than n, NA will be returned as row mean value.

Value

  • A vector with row mean values of df for those rows with at least n valid values. Else, NA is returned.

References

  • http://candrea.ch/blog/compute-spss-like-mean-index-variables/{candrea's blog}
  • http://r4stats.com/2014/09/03/adding-the-spss-mean-n-function-to-r/{r4stats.com}

Examples

Run this code
dat <- data.frame(c1 = c(1,2,NA,4),
                  c2 = c(NA,2,NA,5),
                  c3 = c(NA,4,NA,NA),
                  c4 = c(2,3,7,8))
mean_n(dat, 4) # 1 valid return value
mean_n(dat, 3) # 2 valid return values
mean_n(dat, 2)
mean_n(dat, 1) # all means are shown

Run the code above in your browser using DataLab