get.timeinvar: Get the Value of a Time-Invariant Variable for Each Subject

View source: R/get.timeinvar.r

get.timeinvarR Documentation

Get the Value of a Time-Invariant Variable for Each Subject

Description

Function that returns the value of a time-invariant variable for each subject.

Usage

get.timeinvar(x, id, data, na.rm=TRUE)

Arguments

x

argument to specify the time-invariant 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 (default is TRUE).

Details

The function returns the value of a time-invariant variable for each subject from a dataset in long format.

Value

The function returns a vector.

Author(s)

Wolfgang Viechtbauer wvb@wvbauer.com

See Also

check.timeinvar

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,1,4,2, 3,3,3,3,3, 1,1,2,6,4, 1,2,1,3,1))
dat

# get age of each subject
get.timeinvar(age, subj, data=dat)

# age value is sometimes NA for the 2nd subject (not a problem)
dat$age[6:7] <- NA
dat
get.timeinvar(age, subj, data=dat)

# age value is completely missing for the 2nd subject
dat$age[6:10] <- NA
dat
get.timeinvar(age, subj, data=dat)

# also show subjects with missing value for time-invariant variable
get.timeinvar(age, subj, data=dat, na.rm=FALSE)

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