Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: how to automate sorting and how to automate extracting info from a sort
From
Sergiy Radyakin <[email protected]>
To
[email protected]
Subject
Re: st: how to automate sorting and how to automate extracting info from a sort
Date
Wed, 22 Dec 2010 13:54:48 -0500
Hi, Anjanette.
I don't understand why the resulting table is 2 by 865, not 1 by 865.
But see if the following code does what you want (runs in under 6
seconds):
clear all
set matsize 1100
set seed 12345678
set more off
set rmsg on
local r=1000
local c=865
mata st_matrix("M",runiform(`r',`c'))
matrix R=J(1,`c',.)
matrix S=J(1,`c',.)
quietly {
forvalues i=1/`c' {
matrix A=M[1..`r',`i']
svmat A
sum A1
matrix S[1,`i']=r(min)
replace A1=. if A1==r(min) // eliminate the smallest
sum A1
matrix R[1,`i']=r(min) // save the second smallest
drop A1
}
}
display "Smallest:"
mat l S
display "Second-smallest:"
mat l R
Best, Sergiy Radyakin
On Wed, Dec 22, 2010 at 1:21 PM, Anjanette Chan Tack <[email protected]> wrote:
> Hi Statalist --
>
> I was wondering if you large dataset manipulation masters could advise me on a smart way to do the following:
>
> Imagine you have a very large matrix, with 865 columns and thousands of rows. What you want from this matrix is to get the second smallest numeric value of all the rows within a column, for each column. Ultimately, you want to create a table 865 by 2 table, where for each column, you have that one value (which is, again, the second smallest numeric value of all the rows in that column).
>
> To do this in stata the slow way (which is the only way I know), you could start with the first column, sort the values of the rows, pull out the value of the entry in the second row and record it in the 865 by 2 table you want to build. Then you repeat for the second column, the third, and so on. This is a very repetitive process, and mindless. Is there a way to tell stata, or some other program, to sort each column individually, and to pull out the value of the second row, and to produce a nice table with this information for all columns?
>
> Presently I have 8 datasets that I'd have to do this for. So using the slow way, I'd have to do this little operation 865*8 times. You can imagine that I would be very happy to find a way out of doing this by hand. If you have a smart way to automate this process, your help would save me from a very miserable time.
>
> Let me know if you have any suggestions. I have access to stata, so if you can think of a command to automate this with it would be excellent. If there are other programs that could do this work, and they aren't too hard to learn to use for this task, I'd be willing to try that as well. I can also turn these giant matrices into any format (csv, tab delimited, dbf files). The files generated are too large for excel to display them in full, but I can also have them generated in pieces (e.g. the matrix for one dataset broken into 4 files, for example). This wouldn't affect the sorting, as the sorting goes by column.
>
> Thanks for your help!
>
> Anjie.
>
>
> -------------------------------
> Anjanette M. Chan Tack
> PhD student
> University of Chicago Department of Sociology
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/