I've been using the excellent answer here to find the mode for groups with data table. However, I'd also like to find the number of occurrences of the modal value of x for each group of variable y. How can I do this?
Edit: there is a faster way to find mode than in the answer linked above. I can't find the answer I got it from (please edit and link if you do), but it uses this function (and finds multiple modes if they exist):
MultipleMode <- function(x) {
ux <- unique(x)
tab <- tabulate(match(x, ux)); ux[tab == max(tab)]
}
Here is a version which arbitrarily takes only the first mode when there are two:
SingleMode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
I'm now using this as the base code from which I write a function to find the frequency of the mode, as seen below, instead of the answer I linked to above.
You can create a frequency table for each group, which will have the mode (or an arbitrarily selected one of the modes, in the event of two) at the top with the highest frequency. You can then take the maximum frequency of that table to find the number of times the mode occurs, with the following function and code:
mod_count_fun <- function(x) max(table(x))
DT[,modal_count := mod_count_fun(x),by=y]
Hope that helps, self!
Edit: Actually, I found a faster way to do this. Instead, use:
SingleModeVal <- function(x) {
ux <- unique(x)
max(tabulate(match(x, ux)))
}
DT[,modal_count := SingleModeVal(x),by=y]
This will go approximately 10x faster than my previous answer because of its use of tabulate and vectors, and is based off a clever way of calculating modes I will link to in the main post.