LoginSignup
0
0

More than 5 years have passed since last update.

【stata tips】行列から条件を満たす行を抜きとる

Posted at

1. 目的

stataで,行列Aのある列について特定の値をとる行のみを抜き取り,新たな行列Bをつくる.

2. コード

2-1. 行列Aを作成

  • 行列Aを4×3とする.
  • 行名,列名をつける.
mat A = (1,3,4\1,3,5\2,3,4\2,3,5)
mat rownames A = row1 row2 row3 row4
mat colnames A = col1 col2 col3
mat list A
  • 出力:
A[4,3]
      col1  col2  col3
row1     1     3     4
row2     1     3     5
row3     2     3     4
row4     2     3     5

2-2. 特定の行の抜き出し

  • 1列目の値 = 1を満たす行であるrow1, row2のみを抜き出し,新たな行列Bを作成する.
  • mataを使うのが便利っぽい.
mata: st_matrix("B", select(st_matrix("A"), st_matrix("A")[.,1]:== 1))
mat list A
  • 出力:
B[2,3]
    c1  c2  c3
r1   1   3   4
r2   1   3   5

2-3. 行名・列名の変更

  • 行名・列名がデフォルトのr#, c#になってしまったので書き換える.
mata: st_matrixcolstripe("B", st_matrixcolstripe("A"))
mata: st_matrixrowstripe("B",select(st_matrixrowstripe("A"),st_matrix("A")[.,1]:== 1))
mat list B
  • 出力:
B[2,3]
      col1  col2  col3
row1     1     3     4
row2     1     3     5

3. 参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0