[R] newbie query: simple crosstabs
E.Vettorazzi at uke.uni-hamburg.de
Tue Apr 7 23:29:59 CEST 2009
> apply(mydata[,-1], 2, tapply, mydata[,1], function(x)
will calculate the total percentages of "yes" for each sex relative to
the total size, not within sex.
with your data construction, the following should work
#count "yes" for each sex and question
#count gender as denumerators
#simple check that the solution is ok
Jorge Ivan Velez schrieb:
> Dear Donald,
> Assuming that your data is called "mydata", the first column represents the
> gender and the questions are the columns left, something like the following
> should do the job:
> # Some data
> mydata <- matrix(rbinom(1000,1,p=0.5),ncol=10)
> # What you asked -- hopefully ;)
> apply(mydata[,-1], 2, tapply, mydata[,1], function(x) sum(x)/nrow(mydata))
> On Tue, Apr 7, 2009 at 4:33 PM, Donald Braman <dbraman at law.gwu.edu> wrote:
>> I've been playing around with various table tools, trying to construct a
>> fairly simple cross-tab. It shouldn't be hard, but for some reason it
>> turning out to be (for me).
>> If I want to see how many men and how many women agree with a
>> question (coded 1,0), I can do this:
>>> mytable <- table(male, q1.bin) # gender and a binary response variable
>>> prop.table(mytable, 1) # row percentages
>> male 0 1
>> 0 0.3988 0.6012
>> 1 0.2879 0.7121
>> I can repeat that for each of the items I want gender breakdowns for (q2,
>> q3, q4 ....). But what I really want is a table that shows the percentage
>> answering yes (coded as 1) across many, many binary response items. E.g.,
>> male q1.bin q2.bin q3.bin ...
>> 0 0.6012 0.3421 0.9871 ...
>> 1 0.7121 0.6223 0.0198 ...
>> I've tried various combinations of apply & cbind, but to no avail. It would
>> be easy in SPSS crosstabs, but darnit, I want to use R!
>> [[alternative HTML version deleted]]
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide
>> and provide commented, minimal, self-contained, reproducible code.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help