Using transform and plyr to add a counting column to R

I have a two-level data set (say classes nested in schools) and the data set has been encoded

Like this:

School  Class
  A       1
  A       1
  A       2
  A       2
  B       1
  B       1
  B       2
  B       2

But to run the analysis, I need data to have a unique class identifier, regardless of its membership in the school.

School  Class  NewClass
  A       1       1
  A       1       1
  A       2       2
  A       2       2
  B       1       3
  B       1       3
  B       2       4
  B       2       4 

I tried using transform and ddply, but I'm not sure how to keep NewClass constantly increasing by more for each combination of School and Class. I can come up with some inelegant ways to do this, but I'm sure there are very simple solutions that I just can't think of right now. Any help would be appreciated!

+4
source share
2 answers

interaction, , :

transform(dat,nn = as.integer(interaction(Class,School)))
  School Class nn
1      A     1  1
2      A     1  1
3      A     2  2
4      A     2  2
5      B     1  3
6      B     1  3
7      B     2  4
8      B     2  4
+3

data.table:

library(data.table)
dt = as.data.table(your_df)

dt[, NewClass := .GRP, by = list(School, Class)]
dt
#   School Class NewClass
#1:      A     1        1
#2:      A     1        1
#3:      A     2        2
#4:      A     2        2
#5:      B     1        3
#6:      B     1        3
#7:      B     2        4
#8:      B     2        4

.GRP - . , list(School, Class) by, .


, data.table >= 1.9.0 setDT, a data.frame data.table ( ), .tables.

require(data.table) ## >= 1.9.0
setDT(your_df)      ## your_df is now a data.table, changed by reference.
+3

Source: https://habr.com/ru/post/1533202/


All Articles