[R] Counting question

Deepayan Sarkar deepayan at stat.wisc.edu
Fri Jul 30 18:31:06 CEST 2004


On Friday 30 July 2004 10:13, Adair, Laurence wrote:
> Hi All,
>
> 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??

as.data.frame(table(date, id)) would have been useful, except that it 
coerces date and id to be factors. To work around that, you could do:


u.date <- sort(unique(date))
u.id <- sort(unique(id))
mydf <- as.data.frame(table(date = factor(date, levels = u.date),
                            id = factor(id, levels = u.id)))
mydf <- subset(mydf, Freq > 0)

plot(u.date[mydf$date], u.id[mydf$id], col = mydf$Freq)


Hope that helps,

Deepayan




More information about the R-help mailing list