Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Row mode


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Row mode
Date   Thu, 16 May 2013 09:29:53 +0200

On Thu, May 16, 2013 at 8:20 AM, Md. Alauddin Majumder wrote:
> I want to find ROW MODE.

*------------------ begin example ------------------
clear
input Row1   Row2   Row3   Row4
      1      3      1      4
      3      3      4      1
      1      4      2      2
      4      4      3      3
end

preserve
// turn the data in long format
gen id = _n
reshape long Row, i(id) j(rownr)

// find the mode(s)
bys id Row : gen freq = _N
bys id : egen max = max(freq)
gen ismode = freq == max

// display the mode
keep if ismode
bys id Row : keep if _n == 1
list id Row freq, sepby(id)

// return the data to its original form
restore
*------------------- end example -------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index