Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Stas Kolenikov <skolenik@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: matrix references with column names |
Date | Tue, 17 Sep 2013 13:53:04 -0500 |
You can get the row/column numbers with colnumb(b,"age") and rownumb(b,"age") functions. The code looks cumbersome, but it works. You can break this down for readability into local whereagecol = colnumb(b,"age") local whereagerow = colnumb(b,"age") disp b[`whereagerow',`whereagecol'] -- Stas Kolenikov, PhD, PStat (ASA, SSC) -- Senior Survey Statistician, Abt SRBI -- Opinions stated in this email are mine only, and do not reflect the position of my employer -- http://stas.kolenikov.name On Tue, Sep 17, 2013 at 1:14 PM, David Kantor <kantor.d@att.net> wrote: > Hello, > > Suppose I have a matrix b, I want to refer to a single element using the row > and column names: > b["age", "age"] > > This seems to be okay for defining a 1x1 matrix, as in, > mat c = b["age", "age"] > > But it is not okay when I want refer to it as a scalar quantity, such as > disp b["age", "age"] > or > scalar q = b["age", "age"] > > ; I get the error: > "matrix operators that return matrices not allowed in this context" > > But in these contexts, it okay to refer to refer to elements by number: > disp b[2,2] > > So, to refer to an element by row and column names, I seem to be forced to > extract a copy: > mat c = b["age", "age"] > disp c[1,1] > > Is there a better way to do this? > I can imagine searching in the rownames and colnames for the corresponding > indices, but that's a lot of work; it ought to be easier than that. > > I am using Stata 10. Has this changed in Stata 13? > > I note that in my documentation, in [U]14.9, there is an example: > generate sdif - dif / sqrt(V["price", "price"]) > > which can be expected to fail. > > Thanks if anyone can help with this. > --David > > * > * 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/ * * 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/