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
  • 24,624
  • 7
  • 58
  • 106

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
  • 258,963
  • 21
  • 364
  • 487
  • I want `m` to be the same as if I had done `m <- a[3]`. – Old Pro Jun 24 '14 at 23:46
  • OK, I guess that would work, but it seems terribly inefficient. Is there a way to do it with approximately the same efficiency (or better) as `names(which.max(tt))`? – Old Pro Jun 25 '14 at 00:24
  • So you want this? `m <- factor( names(which.max(tt)), levels=levels(a) , ordered=TRUE)` – IRTFM Jun 25 '14 at 00:54
  • Suppose if there are more than one max value, this keeps only the first name. For ex. a <- ordered (c("a", "b", "c", "b", "c", "b", "b","a", "c", "c")). May be factor(names(tt[max(tt)==tt]), levels=levels(a), ordered=TRUE) [1] b c Levels: a < b < c – akrun Jun 25 '14 at 03:11