Re: [R] Counting question

From: Deepayan Sarkar <deepayan_at_stat.wisc.edu>
Date: Sat 31 Jul 2004 - 02:31:06 EST

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



R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jul 31 03:13:39 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:55:23 EST