0

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.

Community
  • 1
  • 1
Old Pro
  • 23,262
  • 4
  • 56
  • 103

1 Answers1

3

It's not clear exactly what you do want. If you want a factor vector of length 4 with the same levels as a:

 m = a[ a %in% names(which.max(tt)) ]

For a length one vector, do the same as above and just take the first one:

m = a[ a %in% names(which.max(tt)) ][1]
m
#--------
[1] c
Levels: a < b < c
> m == a[3]
[1] TRUE

If you want a vector of the same length, then:

 m <- a
 is.na(m) <- ! m %in% names(which.max(tt))
IRTFM
  • 251,731
  • 20
  • 347
  • 472