Hi Eik, You're absolutely right. My bad. Here is the correction of the code I sent:
apply(mydata[,-1], 2, tapply, mydata[,1], function(x) sum(x)/length(x)) Thanks for pointed this out. Best regards, Jorge On Tue, Apr 7, 2009 at 5:29 PM, Eik Vettorazzi < e.vettora...@uke.uni-hamburg.de> wrote: > Hi Jorge, > > apply(mydata[,-1], 2, tapply, mydata[,1], function(x) > sum(x)/nrow(mydata)) > 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 > cnts<-aggregate(mydata[,-1],by=list(mydata[,1]),FUN=sum) > #count gender as denumerators > nums<-table(mydata[,1]) > cbind(sex=cnts[,1],cnts[,-1]/nums) > > #simple check that the solution is ok > prop.table(table(sex=mydata[,"sex"],q1=mydata[,"q1"]),1) > prop.table(table(sex=mydata[,"sex"],q2=mydata[,"q2"]),1) > > > > > 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 >> 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 <dbra...@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]] >> >> ______________________________________________ >> 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]] ______________________________________________ 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.