Search code examples
rexpss

Expss - using variable prefix to create set of variables for means table


Attempted to get package Expss to produce means tables from sets (in a similar way to multiple response tables, whereby you input a prefix common to all variable in the set).

Here is my attempt to solve this below, but this script produces a table with blank output. Is there a way of doing this?

library(expss)
#generate dummy data
q8_1<-rnorm(30,2,2)
q8_2<-rnorm(30,2,1)
df<-data.frame(q8_1,q8_2)

#Use regex to identify variables with Q8 prefix and then list
varssmeanio<-names(df[grep("^Q8", names(df))])
as.list(varssmeanio)
variolistio = calc(data, as.list(varssmeanio))

df %>%
tab_cells(variolistio) %>%
tab_stat_mean(label = "")  %>%
tab_pivot()

Solution

  • There are special functions for variable selection. You can find help about them by typing ?vars in the console. One of them is ..p - it selects variable with perl-style regular expressions. So we have:

    library(expss)
    #generate dummy data
    q8_1<-rnorm(30,2,2)
    q8_2<-rnorm(30,2,1)
    df<-data.frame(q8_1,q8_2)
    
    df %>%
        tab_cells(..p("^q8")) %>%   # 'p' means 'perl' 
        tab_stat_mean(label = "")  %>%
        tab_pivot()