1
votes

I'm new to R and I don't know all basic concepts yet. The task is to produce a one merged table with multiple response sets. I am trying to do this using expss library and a loop.

This is the code in R without a loop (works fine):

#libraries
#blah, blah...

#path
df.path = "C:/dataset.sav"

#dataset load
df = read_sav(df.path)

#table
table_undropped1 = df %>%
  tab_cells(mdset(q20s1i1 %to% q20s1i8)) %>%
  tab_total_row_position("none") %>%
  tab_stat_cpct() %>%
  tab_pivot()

There are 10 multiple response sets therefore I need to create 10 tables in a manner shown above. Then I transpose those tables and merge. To simplify the code (and learn something new) I decided to produce tables using a loop. However nothing works. I'd looked for a solution and I think the most close to correct one is:

#this generates a message: '1' not found
for(i in 1:10) {
  assign(paste0("table_undropped",i),1) = df %>%
    tab_cells(mdset(assign(paste0("q20s",i,"i1"),1) %to% assign(paste0("q20s",i,"i8"),1)))
    tab_total_row_position("none") %>%
    tab_stat_cpct() %>%
    tab_pivot()
}

Still it causes an error described above the code.

Alternatively, an SPSS macro for that would be (published only to better express the problem because I have to avoid SPSS):

define macro1 (x = !tokens (1)
/y = !tokens (1))

!do !i = !x !to !y.

mrsets
/mdgroup name = !concat($SET_,!i)
variables = !concat("q20s",!i,"i1") to !concat("q20s",!i,"i8")
value = 1.

ctables
/table !concat($SET_,!i) [colpct.responses.count pct40.0].

!doend
!enddefine.

*** MACRO CALL.
macro1 x = 1 y = 10.

In other words I am looking for a working substitute of !concat() in R.

1

1 Answers

0
votes

%to% is not suited for parametric variable selection. There is a set of special functions for parametric variable selection and assignment. One of them is mdset_t:

for(i in 1:10) {
    table_name = paste0("table_undropped",i) 
    ..$table_name = df %>%
        tab_cells(mdset_t("q20s{i}i{1:8}")) %>% # expressions in the curly brackets will be evaluated and substituted 
        tab_total_row_position("none") %>%
        tab_stat_cpct() %>%
        tab_pivot()
}

However, it is not good practice to store all tables as separate variables in the global environment. Better approach is to save all tables in the list:

all_tables = lapply(1:10, function(i)
                    df %>%
                        tab_cells(mdset_t("q20s{i}i{1:8}")) %>% 
                        tab_total_row_position("none") %>%
                        tab_stat_cpct() %>%
                        tab_pivot()
                    )

UPDATE. Generally speaking, there is no need to merge. You can do all your work with tab_*:

my_big_table = df %>%
    tab_total_row_position("none")

for(i in 1:10) {
    my_big_table = my_big_table %>%
        tab_cells(mdset_t("q20s{i}i{1:8}")) %>% # expressions in the curly brackets will be evaluated and substituted 
        tab_stat_cpct() 
}

my_big_table = my_big_table %>%
    tab_pivot(stat_position = "inside_columns") # here we say that we need combine subtables horizontally