The function max()
operates correctly on column of type ordered factor. However, the same operation fails when the column is grouped with by=
.
Let's say I have a data.table as:
DT <- data.table(ID=rep(1:3, 3), State=sample(LETTERS[1:3], 9, replace=TRUE))
Convert the column State
to ordered factor as:
DT[, State := factor(State, levels=LETTERS[1:3], ordered = TRUE)]
This works:
DT[, max(State)]
This fails with error:
DT[, max(State), by="ID"]
Error is: Error in gmax(State) : max is not meaningful for factors.
How come?
DT[, min(ordered(State)), by="ID"]
– Sun BeeDT[, max(ordered(State)), by="ID"]
is giving me an error, butDT[, State[which.max(as.numeric(State))], by = ID]
works. Not sure whyDT[, max(State), by="ID"]
gives an error though, especially sinceDT[, class(State), by = ID]
shows it's still an ordered factor after grouping. – IceCreamToucanDT[, class(State), by = ID]
. Not sure why that happens, or if it's relevant. – Ameya?GForce
. They just haven't coded it for ordered factors yet, I guess. Issue opened over here github.com/Rdatatable/data.table/issues/1947 – Frank