使用Expss包运行表时,变量标签与数据框中的每一行混合(表作为数据框输出)。请参见下面的代码。您会注意到,每个行标签之前都带有变量标签“ Gender”和一个竖线|。字符。如何预防呢?
#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
答案 0 :(得分:0)
此行为是设计使然。如果需要在单独的列中使用变量标签,则可以使用split_columns
函数:
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