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 agree/disagree question (coded 1,0), I can do this:>attach(mydata) >mytable <- table(male, q1.bin) # gender and a binary response variable >prop.table(mytable, 1) # row percentagesq1.bin 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]]
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 set.seed(1) mydata <- matrix(rbinom(1000,1,p=0.5),ncol=10) colnames(mydata)<-c('sex',paste('q',1:9,sep="")) # What you asked -- hopefully ;) apply(mydata[,-1], 2, tapply, mydata[,1], function(x) sum(x)/nrow(mydata)) HTH, Jorge On Tue, Apr 7, 2009 at 4:33 PM, Donald Braman <dbraman@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 > agree/disagree > question (coded 1,0), I can do this: > > >attach(mydata) > >mytable <- table(male, q1.bin) # gender and a binary response variable > >prop.table(mytable, 1) # row percentages > q1.bin > 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@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Tue, 2009-04-07 at 16:33 -0400, Donald Braman 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 agree/disagree > question (coded 1,0), I can do this: > > >attach(mydata) > >mytable <- table(male, q1.bin) # gender and a binary response variable > >prop.table(mytable, 1) # row percentages > q1.bin > 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!Donald, The other solutions is using command CrossTable of package gmodels. -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil