# Most frequent factor in R

#1

I wanted to know how to obtain the most frequent factor in R if the vector is as below:

DT = c(‘a’,‘b’,‘c’,‘a’,‘a’,‘a’,‘b’,‘c’,‘c’,‘c’,‘c’,‘c’)
DT = as.factor(DT)
summary(DT)

#2

Hi

this could help perhaps. Simple enter a vector and it give you a data frame with count and frequency

Alain

# to build a column)

factordistribution<-function(somefactor){
if( class(somefactor) !=“factor”){
somefactor <-factor(somefactor)
}
# More than 100 levels does not make sense
numberlevels <-length(levels(somefactor))
if(numberlevels > 1000){
stop(“More than 1000 levels \n”)
}
numbervalues <-length(somefactor)
temp <-replicate(length(somefactor), c(1))
results <- by(temp, somefactor, sum, simplify = TRUE)
results <- data.frame(Factors= attributes(results)\$dimnames\$somefactor,
Observations = as.vector(results),
stringsAsFactors = FALSE)
reldensity <-sapply(results\$Observations,
function(x) {x/numbervalues})
results <-cbind(results, Density= reldensity )
back <-list(values= numbervalues, numblevels=numberlevels, frequency = results)
return(back)
}