[R] Creating Enumerated Variables

Hadley Wickham hadley at rice.edu
Fri Jul 16 14:48:07 CEST 2010


On Thu, Jul 15, 2010 at 11:08 PM, Dennis Murphy <djmuser at gmail.com> wrote:
> Hi:
>
> I sincerely hope there's an easier way, but one method to get this is as
> follows,
> with d as the data frame name of your test data:
>
> d <- d[order(with(d, Age, School, rev(Grade))), ]
> d$Count <- do.call(c, mapply(seq, 1, as.vector(t(with(d, table(Age,
> School))))))

It's easier if you use plyr ;)

library(plyr)
ddply(d, c("Age", "School"), transform, school_count = seq_along(ID))

Hadley
-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list