Crosstab with multiple items

前端 未结 7 990
天命终不由人
天命终不由人 2021-02-01 10:48

In SPSS, it is (relatively) easy to create a cross tab with multiple variables using the factors (or values) as the table heading. So, something like the following (made up dat

7条回答
  •  无人共我
    2021-02-01 11:17

    The Hmisc package has the summary.formula function that can do something along the lines you want. It is very flexible, so look at the help page for examples, but here is an application to your problem:

    library(Hmisc)
    dd <- data.frame(Q1=sample(1:3, 20, replace=T), Q2=sample(1:3, 20, replace=T), 
                     Q3=sample(1:3, 20, replace=T))  #fake data
    summary(~Q1+Q2+Q3, data=dd, fun=table)
    

    This gives the following result:

     Descriptive Statistics  (N=20)
    
     +------+-------+
     |      |       |
     +------+-------+
     |Q1 : 1|25% (5)|
     +------+-------+
     |    2 |45% (9)|
     +------+-------+
     |    3 |30% (6)|
     +------+-------+
     |Q2 : 1|30% (6)|
     +------+-------+
     |    2 |35% (7)|
     +------+-------+
     |    3 |35% (7)|
     +------+-------+
     |Q3 : 1|35% (7)|
     +------+-------+
     |    2 |30% (6)|
     +------+-------+
     |    3 |35% (7)|
     +------+-------+
    

    The possible values are given in rows, because it has the flexibility of different sets of values for different variables. You might be able to play with the function parameters (like method and fun) to get the other direction.

提交回复
热议问题