data[, x := factor(x)][, N := .N, by = .(x)][order(-N)]
might be closer. If you want unique values of the factor counted in a second column and to order those as well, it would go: by = .(x,y) and order(x,y,-N) as needed.
data[, x := factor(x)][, N := .N, by = .(x)][order(-N)]
might be closer. If you want unique values of the factor counted in a second column and to order those as well, it would go: by = .(x,y) and order(x,y,-N) as needed.
library(data.table)
setDT(data)
data[, .N, by = .(x)][order(N)]
Maybe order(-N) if you want the sorting reversed, and .(factor(x)) if default factor level order is fine and it behaves.
library(data.table)
setDT(data)
data[, .N, by = .(x)][order(N)]
Maybe order(-N) if you want the sorting reversed, and .(factor(x)) if default factor level order is fine and it behaves.