calc.mean | R Documentation |
Function that calculates the mean of a particular variable for each subject.
calc.mean(x, id, data, na.rm=TRUE, expand=FALSE)
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 (default is |
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 |
The function computes the mean of a particular variable for each subject.
When expand=TRUE
, the mean 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’.
A vector.
Wolfgang Viechtbauer wvb@wvbauer.com
calc.mcent
# 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 subject-level means of the stress variable
calc.mean(stress, subj, data=dat)
# add this variable back to the original dataset
dat$mstress <- calc.mean(stress, subj, data=dat, expand=TRUE)
dat
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.