【问题标题】:crosstabs with multiple top-levels rows/cols具有多个顶级行/列的交叉表
【发布时间】:2021-08-01 01:31:32
【问题描述】:

我想生成本质上是多合一交叉表。

我会通过一个例子来解释。我的数据有许多调查问题,q1...q5,每个问题都有四个级别的答案(非常同意、同意、不同意、非常不同意)。我还有四个人口统计变量(性别、地区、年龄组、婚姻状况)。我想生成一个交叉表,基本上显示针对每个人口统计变量的每个问题,如下所示:

           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 代表一个填充有计数/百分比的单元格。

我能找到的所有交叉表函数都只允许在每一层都有一个变量的 n 向表。有没有办法让每个级别都有多个变量,就像我的例子一样?

如果我能在 tidyverse 中以某种方式做到这一点,那将是最好的,但我也愿意接受其他解决方案。

谢谢!

【问题讨论】:

  • 也许你应该考虑创建多个表和列绑定它们

标签: r crosstab


【解决方案1】:

为了将来参考,这是我非常不雅的解决方案,使用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 的每个元素创建一个表,然后将它们绑定到一个“宽”表;然后将每组行绑定到一个完整的表中。 我确信有一种更清洁的方法可以做到这一点......

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2015-07-13
    • 2014-09-25
    • 2014-02-04
    • 2021-06-03
    • 1970-01-01
    相关资源
    最近更新 更多