calc.nomiss: Calculate the Number of Non-Missing Values for Each Subject

View source: R/calc.nomiss.r

calc.nomissR Documentation

Calculate the Number of Non-Missing Values for Each Subject

Description

Function that calculates the number of non-missing values for a particular variable for each subject.

Usage

calc.nomiss(x, id, data, prop=FALSE, expand=FALSE)

Arguments

x

argument to specify the variable.

id

argument to specify a subject id variable.

data

optional data frame that contains the variables specified above.

prop

logical indicating whether to return proportions instead of counts (default is FALSE).

expand

logical indicating whether to expand the returned vector so that it can be added back to the dataset as a new variable (default is FALSE).

Details

The function computes the number (or proportion if prop=TRUE) of non-missing values for a particular variable for each subject.

When expand=TRUE, the number of non-missing values for each subject is repeated in such a way that the returned vector can be added back to the dataset as a new variable. See ‘Examples’.

Value

A vector.

Author(s)

Wolfgang Viechtbauer wvb@wvbauer.com

Examples

# illustrative dataset
dat <- data.frame(subj=rep(1:4, each=5),
                  obs = 1:5,
                  age = rep(c(20,31,27,22), each=5),
                  stress = c(2,3,NA,4,2, 3,3,NA,3,NA, 1,1,2,6,4, 1,2,1,3,1))
dat

# calculate the number of non-missing values per subject for the stress variable
calc.nomiss(stress, subj, data=dat)

# add this variable back to the original dataset
dat$compliance <- calc.nomiss(stress, subj, data=dat, expand=TRUE)
dat

# add the proportion of non-missing values to the dataset
dat$compliance <- calc.nomiss(stress, subj, data=dat, prop=TRUE, expand=TRUE)
dat

wviechtb/esmpack documentation built on April 12, 2025, 9:48 p.m.