# Compute SPSS like mean index variables

Suppose the problem discribed under theanalysisfactor.com.

The point is to compute meaningfull mean index variables while missing values are present. In R you have the switch na.rm  to tell a function – here  the mean()  function –  what to do with missing values. Setting this to true – mean(…, na.rm=TRUE)  – forces the function to use all non-missing values, even if there is only one. In the other case – mean(…, na.rm=FALSE)  – the function will return NA  even if there is ony one missing value.

To handel this situation I have written a very handy function that works like the MEAN.{X}() function in SPSS, where {X} denotes the minimal number of variables that should be non-missing to be incorporated in computing the mean value.

My single line R function looks like

```spss.row.means <- function(vars, not.na=0) {
apply(vars, 1, function(x) ifelse(sum(!is.na(x)) >= not.na, mean(x, na.rm=TRUE), NA))
}```

As the first argument you have to pass the variables (in columns) and the second argument is the minimal number of variables that should be non-missing.

Have fun(ction)!