如何导出 table 仅包含具有 >N 个观察值的单元格?
How can I export a table only containing cells with >N observations?
我想从 Stata 导出几个 tables 到 Excel,但我只对 tables 感兴趣,其中每个单元格都有超过 5
个观察值。有什么办法可以设置条件来做到这一点吗?
使用社区贡献的命令 tabout
我尝试按照以下几行做一些事情:
tabout var1 if n(cell) > 5
对于观测值少于 5
的单元格,我还希望导出的 table 显示另一个值(例如 .
或 *
)而不是 0
。
一般方法是两次通过,一次计算新变量中的细胞频率,另一次以该变量为条件制表。
. sysuse auto
(1978 Automobile Data)
. tab foreign rep78
| Repair Record 1978
Car type | 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
. table foreign rep78
----------------------------------------
| Repair Record 1978
Car type | 1 2 3 4 5
----------+-----------------------------
Domestic | 2 8 27 9 2
Foreign | 3 9 9
----------------------------------------
. bysort foreign rep78 : generate freq = _N
. table foreign rep78 if freq >= 5
----------------------------------
| Repair Record 1978
Car type | 2 3 4 5
----------+-----------------------
Domestic | 8 27 9
Foreign | 9 9
----------------------------------
更通用的代码将支持 if
和 in
条件。
您可以使用 community-contributed 命令导出这样的 table esttab
:
sysuse auto, clear
bysort foreign rep78 : generate freq = _N
quietly tabulate foreign rep78 if freq >= 5, matcell(A)
forvalues i = 1 / 2 {
forvalues j = 1 / 4 {
if A[`i', `j'] == 0 matrix A[`i', `j'] = .
}
}
esttab matrix(A)
----------------------------------------------------------------
A
c1 c2 c3 c4
----------------------------------------------------------------
r1 8 27 9 .
r2 . . 9 9
----------------------------------------------------------------
我想从 Stata 导出几个 tables 到 Excel,但我只对 tables 感兴趣,其中每个单元格都有超过 5
个观察值。有什么办法可以设置条件来做到这一点吗?
使用社区贡献的命令 tabout
我尝试按照以下几行做一些事情:
tabout var1 if n(cell) > 5
对于观测值少于 5
的单元格,我还希望导出的 table 显示另一个值(例如 .
或 *
)而不是 0
。
一般方法是两次通过,一次计算新变量中的细胞频率,另一次以该变量为条件制表。
. sysuse auto
(1978 Automobile Data)
. tab foreign rep78
| Repair Record 1978
Car type | 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
. table foreign rep78
----------------------------------------
| Repair Record 1978
Car type | 1 2 3 4 5
----------+-----------------------------
Domestic | 2 8 27 9 2
Foreign | 3 9 9
----------------------------------------
. bysort foreign rep78 : generate freq = _N
. table foreign rep78 if freq >= 5
----------------------------------
| Repair Record 1978
Car type | 2 3 4 5
----------+-----------------------
Domestic | 8 27 9
Foreign | 9 9
----------------------------------
更通用的代码将支持 if
和 in
条件。
您可以使用 community-contributed 命令导出这样的 table esttab
:
sysuse auto, clear
bysort foreign rep78 : generate freq = _N
quietly tabulate foreign rep78 if freq >= 5, matcell(A)
forvalues i = 1 / 2 {
forvalues j = 1 / 4 {
if A[`i', `j'] == 0 matrix A[`i', `j'] = .
}
}
esttab matrix(A)
----------------------------------------------------------------
A
c1 c2 c3 c4
----------------------------------------------------------------
r1 8 27 9 .
r2 . . 9 9
----------------------------------------------------------------