CK7
CK7

Reputation: 229

How to prevent R Expss from mixing variable name with row label in output data frame?

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

Upvotes: 3

Views: 427

Answers (1)

Gregory Demin
Gregory Demin

Reputation: 4836

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

Upvotes: 1

Related Questions