Find number of occurrences of modal value for a group using data.table [R]

怎甘沉沦 提交于 2020-01-03 16:52:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/29686198/find-number-of-occurrences-of-modal-value-for-a-group-using-data-table-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!