Search code examples
rsequencer-lavaanfactor-analysissem

How to repeat several similar named variables in lavaan model syntax easily?


Suppose we have a data frame like the one below. The numbers are irrelevant to my actual query, so I have simply given them the same value.

df <- data.frame(ABC_1 = c(1,2,3),
                 ABC_2 = c(1,2,3),
                 ABC_3 = c(1,2,3),
                 ABC_4 = c(1,2,3),
                 ABC_5 = c(1,2,3),
                 ABC_6 = c(1,2,3),
                 ABC_7 = c(1,2,3),
                 ABC_8 = c(1,2,3),
                 ABC_9 = c(1,2,3),
                 ABC_10 = c(1,2,3),
                 ABC_11 = c(1,2,3),
                 ABC_12 = c(1,2,3),
                 ABC_13 = c(1,2,3),
                 ABC_14 = c(1,2,3),
                 ABC_15 = c(1,2,3),
                 ABC_16 = c(1,2,3),
                 ABC_17 = c(1,2,3),
                 ABC_18 = c(1,2,3),
                 ABC_19 = c(1,2,3),
                 ABC_20 = c(1,2,3))

Often it is the case that when one has to run a model in lavaan, they have to fit several variables into the model syntax with similar names, such as below:

mod.abc <- '
ABC_Variable =~ ABC_01 + ABC_02 + ABC_03 + ABC_04 + ABC_05 +
ABC_06 + ABC_07 + ABC_08 + ABC_09 + ABC_10 +
ABC_11 + ABC_12 + ABC_13 + ABC_14 + ABC_15 +
ABC_16 + ABC_17 + ABC_18 + ABC_19 + ABC_20 
'

To generate repeating text sequences in R, one can do this easily with a function like this:

rep("ABC",20)

Which gives us a sequence of ABC's:

 [1] "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC"
[12] "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC" "ABC"

However, copying and pasting these into the lavaan model syntax is still time consuming since one has to delete all the quotation marks. Is there a possibly easier way that doesn't require manually writing in the variables?


Solution

  • You can use:

    paste("ABC_variable =~", paste(names(df), collapse = " + "))
    
    [1] "ABC_variable =~ ABC_1 + ABC_2 + ABC_3 + ABC_4 + ABC_5 + ABC_6 + ABC_7 + 
    ABC_8 + ABC_9 + ABC_10 + ABC_11 + ABC_12 + ABC_13 + ABC_14 + ABC_15 + ABC_16 + 
    ABC_17 + ABC_18 + ABC_19 + ABC_20"
    

    Or if I'm building more complex models, I prefer sprintf() which offers more flexibility and readability over multiple nested calls of paste().

    sprintf("ABC_variable =~ %s", paste(names(df), collapse = " + "))