我有一些结果
来自Stata中的tabulate
命令的,但它们显示的数字太详细,如0.4988995。
我想更改summarize
和tabulate
命令输出中的位数。例如,我想看0.0499而不是0.4988995。
有没有办法减少显示的位数?
答案 0 :(得分:1)
不必生成新变量或使用其他命令,例如tabdisp
。 tabulate
命令遵守变量的format
。
考虑以下玩具示例:
sysuse auto, clear
format mpg
variable name display format
-----------------------------
mpg %8.0g
-----------------------------
tabulate rep78 foreign, summarize(mpg) nofreq
Means and Standard Deviations of Mileage (mpg)
Repair |
Record | Car type
1978 | Domestic Foreign | Total
-----------+----------------------+----------
1 | 21 . | 21
| 4.2426407 . | 4.2426407
-----------+----------------------+----------
2 | 19.125 . | 19.125
| 3.7583241 . | 3.7583241
-----------+----------------------+----------
3 | 19 23.333333 | 19.433333
| 4.0856221 2.5166115 | 4.1413252
-----------+----------------------+----------
4 | 18.444444 24.888889 | 21.666667
| 4.5856055 2.7131368 | 4.9348699
-----------+----------------------+----------
5 | 32 26.333333 | 27.363636
| 2.8284271 9.367497 | 8.7323849
-----------+----------------------+----------
Total | 19.541667 25.285714 | 21.289855
| 4.7533116 6.3098562 | 5.8664085
因此,您只需要事先设置所需的format
:
format mpg %8.3g
tabulate rep78 foreign, summarize(mpg) nofreq
Means and Standard Deviations of Mileage (mpg)
Repair |
Record | Car type
1978 | Domestic Foreign | Total
-----------+----------------------+----------
1 | 21 . | 21
| 4.24 . | 4.24
-----------+----------------------+----------
2 | 19.1 . | 19.1
| 3.76 . | 3.76
-----------+----------------------+----------
3 | 19 23.3 | 19.4
| 4.09 2.52 | 4.14
-----------+----------------------+----------
4 | 18.4 24.9 | 21.7
| 4.59 2.71 | 4.93
-----------+----------------------+----------
5 | 32 26.3 | 27.4
| 2.83 9.37 | 8.73
-----------+----------------------+----------
Total | 19.5 25.3 | 21.3
| 4.75 6.31 | 5.87
答案 1 :(得分:0)
您不会提供您使用的代码。
没有一个开关可以做到这一点,只有各种设备。这是一个:
. sysuse auto, clear
(1978 Automobile Data)
. tab for rep78, su(mpg) nost nofreq
Means of Mileage (mpg)
| Repair Record 1978
Car type | 1 2 3 4 5 | Total
-----------+-------------------------------------------------------+----------
Domestic | 21 19.125 19 18.444444 32 | 19.541667
Foreign | . . 23.333333 24.888889 26.333333 | 25.285714
-----------+-------------------------------------------------------+----------
Total | 21 19.125 19.433333 21.666667 27.363636 | 21.289855
. egen mean = mean(mpg), by(for rep78)
. tabdisp for rep78, c(mean) format(%2.1f)
----------------------------------------------
| Repair Record 1978
Car type | 1 2 3 4 5 .
----------+-----------------------------------
Domestic | 21.0 19.1 19.0 18.4 32.0 23.3
Foreign | 23.3 24.9 26.3 14.0
----------------------------------------------
进一步注意tabstat
会产生summarize
- 类似的结果,但选项format()
。