Using transform and plyr to add a counting column in R -
i have two-level dataset (let's classes nested within schools) , dataset coded
like this:
school class 1 1 2 2 b 1 b 1 b 2 b 2
but run analysis need data have unique class id, regardless of school membership.
school class newclass 1 1 1 1 2 2 2 2 b 1 3 b 1 3 b 2 4 b 2 4
i tried using transform , ddply, i'm not sure how keep newclass continually incrementing larger number each combination of school , class. can think of few inelegant ways this, i'm sure there easy solutions can't think of right now. appreciated!
using interaction
create factor, , coerce integer:
transform(dat,nn = as.integer(interaction(class,school))) school class nn 1 1 1 2 1 1 3 2 2 4 2 2 5 b 1 3 6 b 1 3 7 b 2 4 8 b 2 4
Comments
Post a Comment