具有多个项目的交叉表 [英] Crosstab with multiple items
问题描述
在SPSS中,(相对)使用因子(或值)作为表标题来创建具有多个变量的交叉表比较容易。因此,类似以下内容(组成数据等)。 Q1,Q2和Q3每个人的每个人的数字分别为1、2或3。我只是将这些留作数字,但它们可能是因素,似乎都无法帮助解决问题。
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 data, etc.). Q1, Q2, and Q3 each have either a 1, a 2, or a 3 for each person. I just left these as numbers, but they could be factors, neither seemed to help solve the problem.
1 (very Often) 2 (Rarely) 3 (Never)
Q1. Likes it 12 15 13
Q2. Recommends it 22 11 10
Q3. Used it 22 12 9
在SPSS中,甚至可以请求行,列或总百分比。
In SPSS, one can even request row, column, or total percentages.
我已经尝试过gmodels中的table(),ftable(),xtab(),CrossTable()和descr中的CrossTable(),但是这些都不能处理(afaik)多个变量;他们似乎主要处理1个变量与另一个变量的交叉,而第3个则创建层。
I've tried table(), ftable(), xtab(), CrossTable() from gmodels, and CrossTable() from descr, and none of these can handle (afaik) multiple variables; they mostly seem to handle 1 variable crossed with another variable, and the 3rd creates layers.
是否有一个带有一些很好的交叉制表符/表示例的程序包,我可以使用它想出解决办法?我确定我遗漏了一些简单的内容,感谢您指出我错过的内容。也许我必须将每一行生成为单独的列表,然后制作一个数据框并打印该数据框?
Is there a package with some good cross tabbing/table examples that I could use to figure this out? I'm sure I'm missing something simple, so I appreciate you pointing out what I missed. Perhaps I have to generate each row as a separate list and then make a dataframe and print the dataframe?
更新:我现在在catspec包中发现了ctab(),这也是正确的方向。有趣的是,R与SPSS中的Ctables不一致,它基本上是制表工具,与用于调查研究的旧制表工具不同。 ctab()正在尝试,并且是令人钦佩的第一步……但是您仍然不能使用它来创建此表(上方)。
UPDATE: I've now discovered ctab() in package catspec, which is also on the right track. It's interesting that R has no consistent equivalent to Ctables in SPSS, which is basically a "tabbing" tool ala the old tabulate tools used for survey research. ctab() is trying, and is an admirable 1st step... but you still can't make this table (above) with it.
推荐答案
Hmisc
包具有 summary.formula
函数,该函数可以按照您想要的方式进行操作。它非常灵活,请查看帮助页面中的示例,但这是一个解决您问题的应用程序:
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)
这给出以下结果:
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.
这篇关于具有多个项目的交叉表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!