Learn R Programming

ryouready (version 0.4)

rowMeans2: Form row means taking into account a minimum number of values required

Description

In the construction of psychometric scales the calculation of a value is sometimes only desired if a minimum number of items contain values. In SPSS it is possible to calculate a mean value only if a minimum number of values are supplied by using the syntax MEAN.MIN with MIN being a numeric value. The function rowMeans2 does the same.

Usage

rowMeans2(x, w, min = 0, na.rm = TRUE)

Arguments

x
A matrix of dataframe whose columns should be averaged.
w
A numerical vector of weights the same length as number of columns in x.
min
The minimum number of values required to calculate the mean value. Otherwise return NA.
na.rm
A logical value indicating whether NA values in x should be stripped before the computation proceeds.

Value

A vector of means.

Details

rowMeans2 is very similary to rowMeans. The differences are that rowMeans2 allows to indicate the minimum number of values that have to be supplied and to weight the columns.

See Also

rowMeans

Examples

Run this code
x <- replicate(3, runif(5))
 x[1:3, 1] <- NA       # add NAs to data
 x[1:2, 2] <- NA
 x[1, 3] <- NA
 x
 rowMeans2(x)          # the same as rowMeans, except that NAs are allowed
 rowMeans2(x, min=2)   # minimum two values to calculate mean
 rowMeans2(x, min=3)   # minimum three values to calculate mean

 # returns numeric(0) if x has zero rows
 d <- x[NULL, ]
 rowMeans2(d)

 # weights for each column
 rowMeans2(x, w=c(1,1,2))

Run the code above in your browser using DataLab