Re: [R] Percentages for categorical data by group

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Fri, 23 May 2008 10:05:20 -0500

on 05/23/2008 09:51 AM Economics Guy wrote:
> I can think of several ways to blunt force hard code what I want but I
> imagine there is a command or two that can be easily combined to do this:
>
> I have a data frame with about 23000 observations. There first variable is
> the group to which the observation belongs (about 500 different groups). The
> second variable is a response for each observation that is a 1,2,3,4 or 5. I
> want to be able to calculate the percentage of each group that choose each
> response. For example I want to know what percentage of group 1 (which may
> have a value of 34456) choose response 1 and so on.
>
> Here is some code I wrote that generates a data frame like the one I have.
>
> pop <- matrix(1:100000)
> groupIDs <- sample(pop,500)
> groupVar <- sample(groupIDs,23000,replace=TRUE)
> responseVar <- sample(1:5,23000,replace=TRUE)
>
> example.data <- data.frame(groupVar,responseVar)
>
> Is there a fast way to calculate these percentages beyond writing loops to
> manually count the responses for each of the groups?
>
> Thanks,
>
> EG

Using:

   table(example.data)

will give you a cross tabulation of the counts of your ResponseVar by each groupVar.

   prop.table(table(example.data), 1)

will give you a row-wise proportion (0 - 1) of the counts of ResponseVar for each groupVar. If you want percentages (0 - 100):

    prop.table(table(example.data), 1) * 100

See ?table and ?prop.table for more information.

HTH, Marc Schwartz



R-help_at_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. Received on Fri 23 May 2008 - 16:35:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 23 May 2008 - 17:30:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive