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)]
Try following code:
After reading the related topics, by trial and error, the following seems to work.... ref: Add a "rank" column to a data frame