3

When running table using Expss package, the variable label gets mixed with each row in the dataframe (tables are output as dataframes). See code below. You will notice that each row label gets prepended with the variable label "Gender" and a bar | character. How to prevent this?

#Required packages
library(magrittr)
require("ggplot2") 
library(expss)

#Create dataframe
gen<-c("Male","Male","Female","Female")
data<-data.frame(gen)
names(data)<-"Q1"

#Apply var label
data = apply_labels(data,
Q1 = "Gender")

#Tabulate
tarb<-data %>%
tab_cells(Q1) %>%
tab_stat_cpct() %>%
tab_pivot()

#View table
tarb

#Check what is in the row_labels column - notice gender | prepended 

tarb$row_labels
CK7
  • 229
  • 1
  • 11

1 Answers1

1

This behavior is by design. If you need variable label in the separate column you can use split_columns function:

library(expss)

#Create dataframe
gen<-c("Male","Male","Female","Female")
data<-data.frame(gen)
names(data)<-"Q1"

#Apply var label
data = apply_labels(data,
                    Q1 = "Gender")

#Tabulate
tarb<-data %>%
    tab_cells(Q1) %>%
    tab_stat_cpct() %>%
    tab_pivot()


tarb = split_columns(tarb)
tarb
#                        #Total
# 1 Gender       Female     50
# 2                Male     50
# 3        #Total cases      4
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • Unfortunately the command 'split_columns(tarb)' doesn't seem to change it. Please check using tarb$row_labels or using a viewer such as R Commander data editor. – CK7 Feb 05 '19 at 07:57
  • @CK7 `tarb = split_columns(tarb); str(tarb)` - works for me. There is no `row_labels` column after the split. – Gregory Demin Feb 05 '19 at 08:19
  • please paste in answer box, as Stackoverflow does not allow me to mark your response as answered/solved if it is in the form of comment. – CK7 Mar 08 '19 at 14:12
  • @GregoryDemin This solution won't help with a table like this: `htmlTable(mpg %>% tab_rows(model %nest% class, total()) %>% tab_cols(fl)%>%tab_stat_sum()%>%tab_pivot())` How to remove the text "cell var" and "Sum" ? – Ben May 06 '20 at 16:00
  • @Ben You need to add label to `tab_sum` and specify cell variables for sum with `tab_cells`: `htmlTable(mpg %>% tab_rows(model %nest% class, total()) %>% tab_cells(???) %>% tab_cols(fl)%>%tab_stat_sum(label = "")%>%tab_pivot())` – Gregory Demin May 06 '20 at 19:40