Given data frame like this
Unit Anything
A 3.4
A1 2.2
A 6.9
A1 1.1
B 2
B 3
Sort by anything, group by unit, add an index
Unit Anything Index
A 3.4 1
A1 2.2 2
A 6.9 2
A1 1.1 1
B 2 1
B 3 2
I know df[order(df$Anything),]
orders by Anything
.
But I can't get a count to work. I tried stuff like
dt = data.table(df)
dt[,count := .N, by = list(Unit)]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…