Bookmark and Share

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: converting table into matrix


From   R Zhang <[email protected]>
To   [email protected]
Subject   Re: st: converting table into matrix
Date   Fri, 28 Mar 2014 19:05:58 -0400

Nick,
I forgot to post the code. Sorry! My real data has over 400*400
dimensions in a stata data format. that is why i can't use simple
matrix command to input data as matrix.


***** my hypothetical data
clear all
input str20 C_industry str20 S_industry int x
Forestrysupport Forestrysupport 0
Forestrysupport Forestrynursery 0
Forestrysupport logging 0
Forestrynursery Forestrysupport 64
Forestrynursery Forestrynursery 1
Forestrynursery logging 1
logging Forestrysupport 7
logging Forestrynursery 29
logging logging 41
end

list

encode C_industry, gen(c)
encode S_industry, gen(s)



drop C_ S_
list

levelsof c, local(levs)
local rows : word count `levs'
matrix A=J(`rows',`rows',.)
matrix list A

forval i=1/`=_N' {
  local r=c[`i']
  local c=s[`i']
  matrix A[`r',`c']=x[`i']
}

matrix list A

*******************************************

my guess is that the best approach is to use a loop to input data into matrix.

my original post indicates the code did not produce the matrix I
wanted. could you please critique?

thanks a lot,

Rochelle


On Fri, Mar 28, 2014 at 3:49 PM, Nick Cox <[email protected]> wrote:
> I don't see that your code produces a matrix at all.
>
> Seems that you would be better off just typing it in directly.
>
> matrix want = (0,0,0\64,1,1\7,29,41)
> matrix rownames want = Forestrysupport Forestrynursery logging
> matrix colnames want = Forestrysupport Forestrynursery logging
>
> Nick
> [email protected]
>
>
> On 28 March 2014 19:37, R Zhang <[email protected]> wrote:
>> Dear all,
>>
>> I have the following sample code to input data from stata (see below
>> datahave) and get an output in matrix form. after that i will compute
>> eigenvalue for this matrix.
>>
>> the code runs, but the output matrix has some elements misplaced. I
>> wonder if someone could help correct it.
>>
>> thanks!
>> ++++++++++++
>> datahave
>> clear all
>> input str20 C_industry str20 S_industry int x
>> Forestrysupport Forestrysupport 0
>> Forestrysupport Forestrynursery 0
>> Forestrysupport logging 0
>> Forestrynursery Forestrysupport 64
>> Forestrynursery Forestrynursery 1
>> Forestrynursery logging 1
>> logging Forestrysupport 7
>> logging Forestrynursery 29
>> logging logging 41
>> end
>> ++++++++++++
>>
>> ++++++++++++
>> matrix want
>>            c1 c2 c3
>> r1        0    0   0
>> r2        64   1   1
>> r3         7    29  41
>>
>>
>> I would like to replace c1,c2,c3 with variable names Forestrysupport
>> Forestrynursery  logging
>>
>> -Rochelle
>> *
>> *   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/
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index