Dear all, I have a vector like this: z = rep(c("M","F"),c(50,60)) How can I get the following frequency table: Sex counts % F 60 54.5 M 50 45.5 I try: DD<- function(data,...) { n <- nobs(data) out <- c(Frequency = n, k = n/length(data)) return(out) } mApply(z,z,DD) but .... Best regards _________________________________________________________________ Insta-le agora o Windows Live Messenger
You might want to look at the CrossTable function in the gmodels package (in the gregmisc bundle). On 21/09/06, lamack lamack <lamac_k at hotmail.com> wrote:> Dear all, I have a vector like this: > > z = rep(c("M","F"),c(50,60)) > > How can I get the following frequency table: > > Sex counts % > F 60 54.5 > M 50 45.5 > > I try: > > DD<- function(data,...) > { > n <- nobs(data) > out <- c(Frequency = n, > k = n/length(data)) > return(out) > } > > > mApply(z,z,DD) but .... > > > Best regards > > _________________________________________________________________ > Insta-le agora o Windows Live Messenger > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP -- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
And look at ?prop.table too. On 21/09/06, lamack lamack <lamac_k at hotmail.com> wrote:> Dear all, I have a vector like this: > > z = rep(c("M","F"),c(50,60)) > > How can I get the following frequency table: > > Sex counts % > F 60 54.5 > M 50 45.5 > > I try: > > DD<- function(data,...) > { > n <- nobs(data) > out <- c(Frequency = n, > k = n/length(data)) > return(out) > } > > > mApply(z,z,DD) but .... > > > Best regards > > _________________________________________________________________ > Insta-le agora o Windows Live Messenger > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
____________________________ Robert W. Baer, Ph.D. Associate Professor Department of Physiology A. T. Still University of Health Science 800 W. Jefferson St. Kirksville, MO 63501-1497 USA ----- Original Message ----- > >> z = rep(c("M","F"),c(50,60)) >How can I get the following frequency table: > > > > Sex counts % > > F 60 54.5 > > M 50 45.5How about: data.frame(table(z),percent=100*as.vector(table(z)/sum(table(z)))) HTH, Rob