Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • replace a variable with elements from a matrix

    Hello Statisticians,

    I have a variable x=.

    From a cross tab if I want to pick up a frequency from the 3x3 table and replace x with this frequency. How can it be done? I also produced a matrix using tab var1 var2, matcell(M)

    mat li M gives

    c1 c2 c3
    r1 45598 116 8
    r2 22797 55 1
    r3 22801 55 8

    Now I want to replace x with 116. How do I do it?


  • #2
    This is fairly easy to answer, but mainly I am wondering why you want to do that, as knowing the context might lead to quite different suggestions.

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . tab foreign rep78, matcell(M)
    
               |                   Repair record 1978
    Car origin |         1          2          3          4          5 |     Total
    -----------+-------------------------------------------------------+----------
      Domestic |         2          8         27          9          2 |        48 
       Foreign |         0          0          3          9          9 |        21 
    -----------+-------------------------------------------------------+----------
         Total |         2          8         30         18         11 |        69 
    
    . mat li M
    
    M[2,5]
        c1  c2  c3  c4  c5
    r1   2   8  27   9   2
    r2   0   0   3   9   9
    
    . gen foo = M[1, 3]
    
    . l foo in 1
    
         +-----+
         | foo |
         |-----|
      1. |  27 |
         +-----+

    Comment


    • #3
      Thank you NIck. I was actually running cross tabs within loop. Then wanted to pick up frequency from those 100s of cross-tabs and replace and empty variable with those frequencies. I simply produced matrix and then replace my empty variable : replace x =M[2,1] in `i' then it put value for each iteration. You have been of great help here. Thanks again.

      Comment


      • #4
        It might be as easy or easier to go

        Code:
        count if x == 2 & y == 3
        and use the saved result r(N). A problem with referring to a particular row and column of that matrix is that it can change in size depending on whether particular values are absent in the data.

        Comment

        Working...
        X