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

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

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,1,4,2, 3,3,3,3,3, 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      1
#> 4     1   4  20      4
#> 5     1   5  20      2
#> 6     2   1  31      3
#> 7     2   2  31      3
#> 8     2   3  31      3
#> 9     2   4  31      3
#> 10    2   5  31      3
#> 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

# get age of each subject
get.timeinvar(age, subj, data=dat)
#>  1  2  3  4 
#> 20 31 27 22 

# age value is sometimes NA for the 2nd subject (not a problem)
dat$age[6:7] <- NA
dat
#>    subj obs age stress
#> 1     1   1  20      2
#> 2     1   2  20      3
#> 3     1   3  20      1
#> 4     1   4  20      4
#> 5     1   5  20      2
#> 6     2   1  NA      3
#> 7     2   2  NA      3
#> 8     2   3  31      3
#> 9     2   4  31      3
#> 10    2   5  31      3
#> 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
get.timeinvar(age, subj, data=dat)
#>  1  2  3  4 
#> 20 31 27 22 

# age value is completely missing for the 2nd subject
dat$age[6:10] <- NA
dat
#>    subj obs age stress
#> 1     1   1  20      2
#> 2     1   2  20      3
#> 3     1   3  20      1
#> 4     1   4  20      4
#> 5     1   5  20      2
#> 6     2   1  NA      3
#> 7     2   2  NA      3
#> 8     2   3  NA      3
#> 9     2   4  NA      3
#> 10    2   5  NA      3
#> 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
get.timeinvar(age, subj, data=dat)
#>  1  3  4 
#> 20 27 22 

# also show subjects with missing value for time-invariant variable
get.timeinvar(age, subj, data=dat, na.rm=FALSE)
#>  1  2  3  4 
#> 20 NA 27 22