[R] Counting question

Gabor Grothendieck ggrothendieck at myway.com
Fri Jul 30 18:40:37 CEST 2004

Adair, Laurence <Laurence.Adair <at> parsons.com> writes:

> Here is something that sounds simple, but I'm having trouble getting it.  I
> have a data frame with two columns, the first is date and the second is
> employee ID.  I'd like to plot date on the horizontal axis, employee ID on
> the vertical axis, and the number of times the employee appears for the
> given date as a color.  I've kluged something where I make a table
> (table(date, id)) and add points to a plot by looping through the rownames
> (employee ids) of the table.  But certainly there is a better way of doing
> this??

In the example below the first three lines create a random test 
data frame with columns date and id.   

The fourth line calculates the counts using table, converts it back 
to a data frame (which has the effect of adding a Freq column) and 
removes any row with a Freq of 0.  

The last two lines load package gregmisc and then plot the data 
using balloonplot from that package.

r <- 100 + sample(5, 25, replace = TRUE)
DF <- data.frame(date = structure(rev(r), class = "Date"), id = r)

DF2 <- subset(as.data.frame(table(DF)),Freq > 0)

with(DF2, balloonplot(date, id, Freq))

More information about the R-help mailing list