stata collapse应用

 use 0624b\1,clear
 collapse (mean) gpa , by(year) 
list
use https://www.stata-press.com/data/r16/college,clear
collapse (mean) mean_gpa=gpa, by(year)
list 
use https://www.stata-press.com/data/r16/college,clear
collapse (mean) gpa hour, by(year)
list 
*使用collapse分组计算统计量

*collapse能够将变量数据按照对应组别转换为多种统计量
h collapse

sysuse auto,clear

*根据维修次数(rep78)计算price的平均值
collapse (mean) price, by (rep78)

★说明:原始数据会丧失

sysuse auto,clear

*根据维修次数(rep78)计算price的平均值,并将新变量命名为pricemean
collapse (mean) pricemean=price, by (rep78)

sysuse auto,clear

★可以同时计算多个变量和多个统计量

collapse (count) n=price (mean) price mpg, by(rep78)
l

*如何判断两列数据是否相同
sysuse auto,clear

egen a = diff (price mpg)
browse a price mpg

 

posted @ 2023-06-24 15:31  myrj  阅读(318)  评论(0编辑  收藏  举报