Function that calculates the standardized values of a particular variable for each subject.

calc.zcent(x, id, data, na.rm=TRUE)

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.

na.rm

logical indicating whether missing values should be removed before computing the means and standard deviations for computing the standardized values (default is TRUE).

Details

The function computes the standardized values of a particular variable for each subject. Note that the values are standardized within subjects (i.e., based on the subject-level means and standard deviations), so this is analogous to ‘within-person mean centering’, except that the centered values are also divided by the person-level standard deviations.

Value

A vector.

Author

Wolfgang Viechtbauer wvb@wvbauer.com

See also

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 standardized values of the stress variable
dat$cstress <- calc.zcent(stress, subj, data=dat)
dat
#>    subj obs age stress    cstress
#> 1     1   1  20      2 -0.7833495
#> 2     1   2  20      3  0.2611165
#> 3     1   3  20     NA         NA
#> 4     1   4  20      4  1.3055824
#> 5     1   5  20      2 -0.7833495
#> 6     2   1  31      3        NaN
#> 7     2   2  31      3        NaN
#> 8     2   3  31     NA         NA
#> 9     2   4  31      3        NaN
#> 10    2   5  31     NA         NA
#> 11    3   1  27      1 -0.8302781
#> 12    3   2  27      1 -0.8302781
#> 13    3   3  27      2 -0.3690125
#> 14    3   4  27      6  1.4760499
#> 15    3   5  27      4  0.5535187
#> 16    4   1  22      1 -0.6708204
#> 17    4   2  22      2  0.4472136
#> 18    4   3  22      1 -0.6708204
#> 19    4   4  22      3  1.5652476
#> 20    4   5  22      1 -0.6708204