I can get the most frequent level or name of a factor in a table using table()
and levels()
or name()
as explained here, but how can I get a factor itself?
> a <- ordered (c("a", "b", "c", "b", "c", "b", "a", "c", "c"))
> tt <- table(a)
> m = names(which.max(tt)) # what do I put here?
> is.factor(m)
[1] FALSE # I want this to be TRUE and for m to be identical a[3]
This is just an example, of course. What I'm really trying to do is a lot of manipulation and aggregation of factors and I just want to keep the factors consistent across all the variables. I don't want them to change levels or order or drop levels because there is no data.