English 中文(简体)
多个物品的交叉
原标题:Crosstab with multiple items

在特别安全局,(相对而言)使用作为表格标题的系数(或数值)很容易形成带有多种变量的交叉表。 因此,如下文(编造数据等)。 Q1、Q2和Q3各有1、2或3人。 我只剩下这些数目,但可能是因素,似乎无助于解决问题。

                        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

在特别安全局,人们甚至可以申请浏览量、一栏或总百分比。

I ve Trial table ()、 ftable()、xtab()、 CrossTable() from gmodels, and CrossTable() from descr, 而其中没有一个能够处理(afaik)多种变数; 它们似乎大多处理1个变数,另一个变数相,第3个产生层。

是否有一揽子计划,其中有一些好的交叉表格/表格例子,我可以用来说明这一点吗? 我确信我没有一件简单的事情,因此,我赞赏你指出我错过的东西。 或许 我必须把每行都编成单独的清单,然后提供数据框架和打印数据框架?

UPDATE: I ve now found ctab() in Pack catspec, which is also on the right path. 令人感兴趣的是,R没有与SPSS的Ctables一致,而这种表格基本上是一种“制表”工具,是用于调查研究的旧的泡沫工具。 ctab(a)正在尝试,是令人钦佩的第一步......但你仍然可以提出这个表(详细)。

问题回答

<代码>Hmisc 包裹有<编码>summary.formula/code>功能,可按照你的意愿做一些事情。 它非常灵活,因此,可以看一看例子的帮助页,但这里适用于你的问题:

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)|
 +------+-------+

可能的数值在各行各业,因为它具有不同变数的不同价值。 您可以按照职能参数(如<代码> 方法和<代码>fun)行事,以获得其他指示。

修改以前的例子

library(Hmisc)
library(plyr)
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

cross <- ldply(describe(dd), function(x) x$values[1,])[-1]

rownames(cross) <- c("Q1. Likes it","Q2. Recommends it","Q3. Used it")
names(cross) <- c("1 (very Often)","2 (Rarely)","3 (Never)")

我们现在看着这样的情况。

> cross
                  1 (very Often) 2 (Rarely) 3 (Never)
Q1. Likes it                   4         10         6
Q2. Recommends it              7          9         4
Q3. Used it                    6          4        10

just check Hadley Wickham s reshape package. AFAIS, you need cast function from the package.

<代码>xtabs有一个公式接口,可以采取某种做法加以利用,但可以这样做。 如果您在数据框<代码>df上有数据,且其变量称为ques<>/code>和resp<> 代码>,请您使用:

xtabs(~ques+resp,data=df)

例如:

> 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

您可在几个表格中使用rbind(),例如:

multitab <- function(...){
   tabs<-list(...)
   tablist<-lapply(tabs,table)
   bigtab<-t(sapply(tablist,rbind))
   bigtab } 




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...