具有多个顶层的交叉表 rows/cols
crosstabs with multiple top-levels rows/cols
我想生成本质上是多合一的交叉表 table。
我会通过一个例子来解释。我的数据有许多调查问题,q1...q5,每个问题都有四个级别的答案(非常同意、同意、不同意、非常不同意)。我还有四个人口统计变量(性别、地区、年龄组、婚姻状况)。我想生成一个交叉表 table,它基本上显示针对每个人口统计变量的每个问题,如下所示:
gender region age group marital status
M F X North South East West <25 25-34 35-44 >=45 Single Married Divorced Widowed
Q1 1 N N N N N N N N N N N N N N N
2 N N N N N N N N N N N N N N N
3 N N N N N N N N N N N N N N N
4 N N N N N N N N N N N N N N N
Q2 1 N N N N N N N N N N N N N N N
2 N N N N N N N N N N N N N N N
3 N N N N N N N N N N N N N N N
4 N N N N N N N N N N N N N N N
等...
每个 N 代表一个单元格 count/percentage.
我能找到的所有交叉表函数都只允许 n 向 tables,每个级别有一个变量。有没有办法在每个级别都有多个变量,就像我的例子一样?
如果我能以某种方式在 tidyverse 中做到这一点,那将是最好的,但我也愿意接受其他解决方案。
谢谢!
为了将来参考,这是我非常不优雅的解决方案,使用 tidyverse
:
yelements<-c("Q1", "Q2", "Q3") # etc...
xelements<-c("region","gender","age_group","martial_status")
Rows<-NULL # a helper table to create each set of rows individually
FullXTab<-NULL
for (i in yelements) { # this is a vector of names of factor columns that will form the rows of the xtabs
for (w in xelements) { # vector of names of factor columns that will form the columns of the xtabs
x<-ftable(data[c(i,w)])
x<-as.data.frame(as.matrix(x))
names(x)<-paste(w,names(x),sep="_") # add the name of the variable to the names of each of the levels that will form individual columns to differentiate them
names(x)<-gsub("V1","NA",names(x)) # blank items will turn into meaningless "V1" columns, so I replace that with NA
if(is.null(Rows)) {
x<-rownames_to_column(x,"answer") # make the y-axis factor levels into their own column
Rows<-x
} else {Rows<-bind_cols(Rows,x)}
}
Rows$Q<-i # create a column with the name of the y-axis vector, to differentiate different vectors with similar levels, e.g. question numbers
if(is.null(FullXTab)) {FullXTab<-Rows} else {FullXTab<-bind_rows(FullXTab,Rows)}
Rows<-NULL
}
这首先为 xelements
中的第一个元素创建了一组行,yelements
中的每个元素都有一个 table,然后将它们绑定到一个“宽”table;然后将每组行绑定到一个完整的 table。
我确信有一种更简洁的方法可以做到这一点...
我想生成本质上是多合一的交叉表 table。
我会通过一个例子来解释。我的数据有许多调查问题,q1...q5,每个问题都有四个级别的答案(非常同意、同意、不同意、非常不同意)。我还有四个人口统计变量(性别、地区、年龄组、婚姻状况)。我想生成一个交叉表 table,它基本上显示针对每个人口统计变量的每个问题,如下所示:
gender region age group marital status
M F X North South East West <25 25-34 35-44 >=45 Single Married Divorced Widowed
Q1 1 N N N N N N N N N N N N N N N
2 N N N N N N N N N N N N N N N
3 N N N N N N N N N N N N N N N
4 N N N N N N N N N N N N N N N
Q2 1 N N N N N N N N N N N N N N N
2 N N N N N N N N N N N N N N N
3 N N N N N N N N N N N N N N N
4 N N N N N N N N N N N N N N N
等...
每个 N 代表一个单元格 count/percentage.
我能找到的所有交叉表函数都只允许 n 向 tables,每个级别有一个变量。有没有办法在每个级别都有多个变量,就像我的例子一样?
如果我能以某种方式在 tidyverse 中做到这一点,那将是最好的,但我也愿意接受其他解决方案。
谢谢!
为了将来参考,这是我非常不优雅的解决方案,使用 tidyverse
:
yelements<-c("Q1", "Q2", "Q3") # etc...
xelements<-c("region","gender","age_group","martial_status")
Rows<-NULL # a helper table to create each set of rows individually
FullXTab<-NULL
for (i in yelements) { # this is a vector of names of factor columns that will form the rows of the xtabs
for (w in xelements) { # vector of names of factor columns that will form the columns of the xtabs
x<-ftable(data[c(i,w)])
x<-as.data.frame(as.matrix(x))
names(x)<-paste(w,names(x),sep="_") # add the name of the variable to the names of each of the levels that will form individual columns to differentiate them
names(x)<-gsub("V1","NA",names(x)) # blank items will turn into meaningless "V1" columns, so I replace that with NA
if(is.null(Rows)) {
x<-rownames_to_column(x,"answer") # make the y-axis factor levels into their own column
Rows<-x
} else {Rows<-bind_cols(Rows,x)}
}
Rows$Q<-i # create a column with the name of the y-axis vector, to differentiate different vectors with similar levels, e.g. question numbers
if(is.null(FullXTab)) {FullXTab<-Rows} else {FullXTab<-bind_rows(FullXTab,Rows)}
Rows<-NULL
}
这首先为 xelements
中的第一个元素创建了一组行,yelements
中的每个元素都有一个 table,然后将它们绑定到一个“宽”table;然后将每组行绑定到一个完整的 table。
我确信有一种更简洁的方法可以做到这一点...