File: maxCat.R

package info (click to toggle)
r-cran-vim 6.2.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,556 kB
  • sloc: cpp: 141; sh: 12; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 789 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#' Aggregation function for a factor variable
#'
#' The function maxCat chooses the level
#' with the most occurrences and random if the maximum is not unique.
#'
#' @param x factor vector
#' @param weights numeric vector providing weights for the observations in x
#' @export


maxCat <- function(x,weights = NULL){
  #choose cat with max prob, random if max is not unique
  is_logical <- is.logical(x)
  if(!is.factor(x))
    x <- as.factor(x)
  s <- summary(x)
  s <- s[s!=0]
  if(!is.null(weights)){
    tmpTab <- merge(aggregate(weights,list(x),sum), data.frame("Group.1"=names(s),prob=s))
    s <- tmpTab$prob*tmpTab$x
    names(s) <- tmpTab$Group.1
  }
  if(sum(s>0)>1)
    s <- sample(s)
  if (is_logical)
    as.logical(names(s)[which.max(s)])
  else
    names(s)[which.max(s)]
}