1

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()
CK7
  • 229
  • 1
  • 11

2 Answers2

2

This will work

varssmeanio<-df[grep("[Q8]", names(df))]

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

Output Table

 |      | #Total |
 | ---- | ------ |
 | q8_1 |    1.8 |
 | q8_2 |    1.6 |
Mohanasundaram
  • 2,889
  • 1
  • 8
  • 18
0

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()
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20