[R] Count data categories from table
David Winsemius
dwinsemius at comcast.net
Fri Jul 17 14:51:58 CEST 2009
On Jul 17, 2009, at 5:47 AM, Miroslav Nikolov wrote:
>
> Hi there,
>
> I have a relatively simple question, though, I couldn't find a
> solution for
> it so far. I have a table with 1000 entries and columns containing
> information about different parameters for each entry.
> What I want to do is group all parameters from one of the columns
> [e.g. if
> all 1000 entries are grouped in 30 different categories (described as
> character strings) in a second column] and have a pie chart
> describing the
> distribution of all 1000 entries into these 30 categories.
> The problem I have is to make R count how many times each of the 30
> categories is present in the table; then if I have them counted
> (e.g. if I
> have category1 - 234 times, category2 - 356 times, etc. in a vector/
> table)
> the rest will be easier.
Several options are available. An example dataset would have made
understanding your setup much clearer. I am having trouble parsing
your natural language presentation of the problem.
Perhaps these examples will help:
> y <- data.frame(x=sample(LETTERS[1:5], 20, replace=TRUE) )
> table(y$x)
A B C D E
3 5 2 7 3
> xtabs(~x, data=y)
x
A B C D E
3 5 2 7 3
> ?tapply
> tapply(y$x, y$x, length)
A B C D E
3 5 2 7 3
Pie charts are deprecated on this list, so it's not surprising you
have difficulty finding examples, but surely you can find worked
examples, nonetheless. The search sites to consult include:
http://search.r-project.org/nmz.html
http://addictedtor.free.fr/graphiques/
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list