Crosstab with multiple items

前端 未结 7 1091
天命终不由人
天命终不由人 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 10:56

    xtabs has a formula interface that can take some practice to get used to, but this can be done. If you have the data in a dataframe df and your variables are called ques and resp, you can use:

    xtabs(~ques+resp,data=df)
    

    For example:

    > t1 <- rep(c("A","B","C"),5)
    > t2 <- rpois(15,4)
    > df <- data.frame(ques=t1,resp=t2)
    > xtabs(~ques+resp,data=df)
         resp
    names 2 3 4 5 6 7 9
        A 1 0 2 1 0 0 1
        B 1 0 0 2 1 1 0
        C 1 2 0 1 0 1 0
    

提交回复
热议问题