Skip to contents

Merges variables with corresponding case names from a data.frame with an scdf.

Usage

add_l2(scdf, data_l2, cvar = "case")

Arguments

scdf

A single-case data frame. See scdf() to learn about this format.

data_l2

A level 2 dataset.

cvar

Character string with the name of the "case" variable in the L2 dataset (default is 'case').

Value

An scdf with added level-2 variables.

Details

This function is mostly used in combination with the hplm() function. It adds level-2 variables to each single-case data frame in an scdf based on matching case names.

Author

Juergen Wilbert

Examples

## Example with the default case variable name 'case'
Leidig2018 |> add_l2(Leidig2018_l2)
#> #A single-case data frame with 35 cases
#> 
#>  1a1: academic_engagement mt classID weekday disruptive_behavior phase class
#>                         4  1      1a       3                   1     A    1a
#>                         1  2      1a       4                   1     A    1a
#>                         2  3      1a       5                   1     A    1a
#>                      <NA>  4      1a       1                <NA>     A    1a
#>                         2  5      1a       2                   1     A    1a
#>                         3  6      1a       3                   1     A    1a
#>                         1  7      1a       4                   1     A    1a
#>                         1  8      1a       5                   2     A    1a
#>                         3  9      1a       1                   0     B    1a
#>                         4 10      1a       2                   0     B    1a
#>                         3 11      1a       3                   0     B    1a
#>                         4 12      1a       4                   1     B    1a
#>                         4 13      1a       5                   0     B    1a
#>                         4 14      1a       1                   0     B    1a
#>                         4 15      1a       2                   0     B    1a
#>  gender migration first_language_german SDQ_TOTAL SDQ_EXTERNALIZING
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>  SDQ_INTERNALIZING ITRF_TOTAL ITRF_ACADEMIC ITRF_BEHAVIOR
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#> # ... up to 93 more rows
#> #  34 more cases
## Example with a different case variable name in the L2 data
Leidig2018_l2_renamed <- Leidig2018_l2
names(Leidig2018_l2_renamed)[2] <- "subject"
Leidig2018 |> add_l2(Leidig2018_l2_renamed, cvar = "subject")
#> #A single-case data frame with 35 cases
#> 
#>  1a1: academic_engagement mt classID weekday disruptive_behavior phase class
#>                         4  1      1a       3                   1     A    1a
#>                         1  2      1a       4                   1     A    1a
#>                         2  3      1a       5                   1     A    1a
#>                      <NA>  4      1a       1                <NA>     A    1a
#>                         2  5      1a       2                   1     A    1a
#>                         3  6      1a       3                   1     A    1a
#>                         1  7      1a       4                   1     A    1a
#>                         1  8      1a       5                   2     A    1a
#>                         3  9      1a       1                   0     B    1a
#>                         4 10      1a       2                   0     B    1a
#>                         3 11      1a       3                   0     B    1a
#>                         4 12      1a       4                   1     B    1a
#>                         4 13      1a       5                   0     B    1a
#>                         4 14      1a       1                   0     B    1a
#>                         4 15      1a       2                   0     B    1a
#>  gender migration first_language_german SDQ_TOTAL SDQ_EXTERNALIZING
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>       0         0                     1        10                 9
#>  SDQ_INTERNALIZING ITRF_TOTAL ITRF_ACADEMIC ITRF_BEHAVIOR
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#>                  1         11             7             4
#> # ... up to 93 more rows
#> #  34 more cases