Don't know if this is more elegant:
names(which.max(table(x)))
Andy
From: Rajarshi Guha
Hi, I have a factor and I would like to find the most frequent level.
I think my current approach is a bit long winded and I was
wondering if
there was a more elegant way to do it:
x -
Rajarshi Guha wrote:
Hi, I have a factor and I would like to find the most frequent level.
I think my current approach is a bit long winded and I was wondering if
there was a more elegant way to do it:
x - factor(sample(1:0, 5,replace=TRUE))
levels(x)[ which( as.logical((table(x) ==
you could try this:
x - factor(sample(letters[1:3], 20, TRUE))
###
tab - table(x)
names(tab)[tab == max(tab)]
I hope it helps.
Best,
Dimitris
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35,
You could also use:
names(rev(sort(table(x[1]
There is nonetheless a difference if there are several levels which
provides this maximum.
This method will only return one, yours would return all those levels
(which may not be desirable for some others processing).
HTH,
Eric
Eric Lecoutre
The which.max solution is fine as long as the maximum is always unique.
Otherwise, which.max will give you the first maximum.
So using the x == max(x) version will have an advantage if there can be
ties.
Regards,
Matt Wiener
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL