CK7
CK7

Reputation: 229

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()

Upvotes: 1

Views: 164

Answers (2)

Gregory Demin
Gregory Demin

Reputation: 4846

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()

Upvotes: 0

Mohanasundaram
Mohanasundaram

Reputation: 2949

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 |

Upvotes: 2

Related Questions