RE: [R] a more elegant approach to getting the majority level
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 - factor(sample(1:0, 5,replace=TRUE)) levels(x)[ which( as.logical((table(x) == max(table(x == TRUE ) ] (The length of x will always be an odd number, so I wont get a tie in max()) Thanks, --- Rajarshi Guha [EMAIL PROTECTED] http://jijo.cjb.net GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE --- Alcohol, an alternative to your self - 'Alcohol' by the Bare Naked Ladies __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] a more elegant approach to getting the majority level
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) == max(table(x == TRUE ) ] (== TRUE) can ALWAYS be omitted, see also: library(fortunes) fortune(TRUE) x == max(x) should be replaced by which.max(x) as.logical() is superfluous Hence we get: names(which.max(table(x))) Uwe Ligges (The length of x will always be an odd number, so I wont get a tie in max()) Thanks, --- Rajarshi Guha [EMAIL PROTECTED] http://jijo.cjb.net GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE --- Alcohol, an alternative to your self - 'Alcohol' by the Bare Naked Ladies __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] a more elegant approach to getting the majority level
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, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm - Original Message - From: Rajarshi Guha [EMAIL PROTECTED] To: R r-help@stat.math.ethz.ch Sent: Thursday, May 26, 2005 4:36 PM Subject: [R] a more elegant approach to getting the majority level 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) == max(table(x == TRUE ) ] (The length of x will always be an odd number, so I wont get a tie in max()) Thanks, --- Rajarshi Guha [EMAIL PROTECTED] http://jijo.cjb.net GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE --- Alcohol, an alternative to your self - 'Alcohol' by the Bare Naked Ladies __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] a more elegant approach to getting the majority level
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 UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 [EMAIL PROTECTED] http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rajarshi Guha Sent: jeudi 26 mai 2005 16:37 To: R Subject: [R] a more elegant approach to getting the majority level 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) == max(table(x == TRUE ) ] (The length of x will always be an odd number, so I wont get a tie in max()) Thanks, --- Rajarshi Guha [EMAIL PROTECTED] http://jijo.cjb.net GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE --- Alcohol, an alternative to your self - 'Alcohol' by the Bare Naked Ladies __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] a more elegant approach to getting the majority level
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 PROTECTED] On Behalf Of Uwe Ligges Sent: Thursday, May 26, 2005 10:57 AM To: [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Subject: Re: [R] a more elegant approach to getting the majority level 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) == max(table(x == TRUE ) ] (== TRUE) can ALWAYS be omitted, see also: library(fortunes) fortune(TRUE) x == max(x) should be replaced by which.max(x) as.logical() is superfluous Hence we get: names(which.max(table(x))) Uwe Ligges (The length of x will always be an odd number, so I wont get a tie in max()) Thanks, --- Rajarshi Guha [EMAIL PROTECTED] http://jijo.cjb.net GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE --- Alcohol, an alternative to your self - 'Alcohol' by the Bare Naked Ladies __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html