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

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

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
#>    subj obs age stress
#> 1     1   1  20      2
#> 2     1   2  20      3
#> 3     1   3  20     NA
#> 4     1   4  20      4
#> 5     1   5  20      2
#> 6     2   1  31      3
#> 7     2   2  31      3
#> 8     2   3  31     NA
#> 9     2   4  31      3
#> 10    2   5  31     NA
#> 11    3   1  27      1
#> 12    3   2  27      1
#> 13    3   3  27      2
#> 14    3   4  27      6
#> 15    3   5  27      4
#> 16    4   1  22      1
#> 17    4   2  22      2
#> 18    4   3  22      1
#> 19    4   4  22      3
#> 20    4   5  22      1

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

# add this variable back to the original dataset
dat$compliance <- calc.nomiss(stress, subj, data=dat, expand=TRUE)
dat
#>    subj obs age stress compliance
#> 1     1   1  20      2          4
#> 2     1   2  20      3          4
#> 3     1   3  20     NA          4
#> 4     1   4  20      4          4
#> 5     1   5  20      2          4
#> 6     2   1  31      3          3
#> 7     2   2  31      3          3
#> 8     2   3  31     NA          3
#> 9     2   4  31      3          3
#> 10    2   5  31     NA          3
#> 11    3   1  27      1          5
#> 12    3   2  27      1          5
#> 13    3   3  27      2          5
#> 14    3   4  27      6          5
#> 15    3   5  27      4          5
#> 16    4   1  22      1          5
#> 17    4   2  22      2          5
#> 18    4   3  22      1          5
#> 19    4   4  22      3          5
#> 20    4   5  22      1          5

# add the proportion of non-missing values to the dataset
dat$compliance <- calc.nomiss(stress, subj, data=dat, prop=TRUE, expand=TRUE)
dat
#>    subj obs age stress compliance
#> 1     1   1  20      2        0.8
#> 2     1   2  20      3        0.8
#> 3     1   3  20     NA        0.8
#> 4     1   4  20      4        0.8
#> 5     1   5  20      2        0.8
#> 6     2   1  31      3        0.6
#> 7     2   2  31      3        0.6
#> 8     2   3  31     NA        0.6
#> 9     2   4  31      3        0.6
#> 10    2   5  31     NA        0.6
#> 11    3   1  27      1        1.0
#> 12    3   2  27      1        1.0
#> 13    3   3  27      2        1.0
#> 14    3   4  27      6        1.0
#> 15    3   5  27      4        1.0
#> 16    4   1  22      1        1.0
#> 17    4   2  22      2        1.0
#> 18    4   3  22      1        1.0
#> 19    4   4  22      3        1.0
#> 20    4   5  22      1        1.0