Suppose my data looks like this: Obs, Male, Female, Height 1, T, F, 66 2, F, T, 64 3, T, F, 59 4, T, F, 55 5, F, T, 62 6, T, F, 67 7, T, F, 58 8, T, F, 57 9, F, T, 58 10, T, F, 62 How can I find the percentage of Males with a Height of over 60? Likewise, how can I find the percentage of Females with a Height of over 60? Thank you very much. Geoff [[alternative HTML version deleted]]
Try this: prop.table(table(subset(x, Height > 60, select = Male:Female))) On Sun, May 2, 2010 at 2:58 PM, Geoffrey Smith <gps@asu.edu> wrote:> Suppose my data looks like this: > > Obs, Male, Female, Height > 1, T, F, 66 > 2, F, T, 64 > 3, T, F, 59 > 4, T, F, 55 > 5, F, T, 62 > 6, T, F, 67 > 7, T, F, 58 > 8, T, F, 57 > 9, F, T, 58 > 10, T, F, 62 > > How can I find the percentage of Males with a Height of over 60? Likewise, > how can I find the percentage of Females with a Height of over 60? > > Thank you very much. > > Geoff > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
how about: d=data.frame(ht=rnorm(20,60,20),sex=sample(0:1,20,rep=T)); d with(d,by(ht,sex,mean)) with(d,by(ht,sex,function(x)mean(x>=60))) hth, david freedman -- View this message in context: http://r.789695.n4.nabble.com/percent-by-subset-tp2123057p2123079.html Sent from the R help mailing list archive at Nabble.com.
On May 2, 2010, at 1:58 PM, Geoffrey Smith wrote:> Suppose my data looks like this: > > Obs, Male, Female, Height > 1, T, F, 66 > 2, F, T, 64 > 3, T, F, 59 > 4, T, F, 55 > 5, F, T, 62 > 6, T, F, 67 > 7, T, F, 58 > 8, T, F, 57 > 9, F, T, 58 > 10, T, F, 62 > > How can I find the percentage of Males with a Height of over 60? > Likewise, > how can I find the percentage of Females with a Height of over 60?R-help expects you to read the introductory materials offered at CRAN, of which there are quite a few examples, before posting questions, ... especially when they look like homework. Here's what you should do: Get this into R using the read functions, read the Posting Guide to see one method of creating reproducible versions of R objects or look at the dput function for another method. Then and only then post your homework question as reprocucible R code and further hints will perhaps be offered.>-- David Winsemius, MD West Hartford, CT