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)]
After reading the related topics, by trial and error, the following seems to work....
ref: Add a "rank" column to a data frame
df <- read.table(header = TRUE, text = "
Unit Anything
A 3.4
A1 2.2
A 6.9
A1 1.1
B 2
B 3
")
df <- transform(df, x= ave(Anything,Unit,FUN=function(x) order(x,decreasing=F)))
df
Unit Anything x
1 A 3.4 1
2 A1 2.2 2
3 A 6.9 2
4 A1 1.1 1
5 B 2.0 1
6 B 3.0 2
Try following code:
ddf = structure(list(Unit = structure(c(1L, 2L, 1L, 2L, 3L, 3L), .Label = c("A",
"A1", "B"), class = "factor"), Anything = c(3.4, 2.2, 6.9, 1.1,
2, 3)), .Names = c("Unit", "Anything"), class = "data.frame", row.names = c(NA,
-6L))
ddf2 = ddf[order(ddf$Unit, ddf$Anything),]
ddf2$index = 0
for(U in unique(ddf$Unit)){
ddf2[ddf2$Unit==U,]$index = 1:length(ddf2[ddf2$Unit==U,]$Unit)
}
ddf2
Unit Anything index
1 A 3.4 1
3 A 6.9 2
4 A1 1.1 1
2 A1 2.2 2
5 B 2.0 1
6 B 3.0 2