Function that calculates the summary statistic for a particular variable for each subject.

calc.fun(x, id, data, FUN, 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.

FUN

function that computes the summary statistic of interest.

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).

...

other arguments passed on to the function specified via FUN.

Details

The function computes the summary of a particular variable for each subject.

When expand=TRUE, the value of the statistic 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

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 subject-level SDs of the stress variable
calc.fun(stress, subj, data=dat, FUN=sd)
#>         1         2         3         4 
#>        NA        NA 2.1679483 0.8944272 

# remove the missings before computing the SDs
calc.fun(stress, subj, data=dat, FUN=sd, na.rm=TRUE)
#>         1         2         3         4 
#> 0.9574271 0.0000000 2.1679483 0.8944272 

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