全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
1828 3
2014-02-18
悬赏 30 个论坛币 已解决
现在有模拟数据:
samplex_1 x_2
11 50
11 41
13 38
14 60
11065
13 71
12 21
12 30
11 40
16 66
找出sample里面top5(出现次数最多的前5个)x_1的x_2的平均值。如x_1=1,meanx_2=(50+41+40)/3
理想输出结果:
sampletop5x_1 meanx_2
11 (50+41+40)/3
12 (21+30)/2
13 (71+38)/2
14 60
16 66
各位这东西能用stata处理么TT?

最佳答案

hplcdadong 查看完整内容

gen x3=x1 collapse (count) x3 (mean) x2, by(sample x1) gsort sample -x3 x1 by sample: keep if _n
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-2-18 02:37:03
gen x3=x1
collapse (count) x3 (mean) x2, by(sample x1)
gsort sample -x3 x1
by sample: keep if _n<=5
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-2-18 04:22:03
bys sample x1: egen meanx2=mean(x2)
bys sample: egen countx1=count(x1)
duplicates drop sample x1 countx1, force
bys sample: egen rankx1=rank(countx1), unique
keep if rankx1<=5
sort sample rankx1
keep sample x1 meanx2
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-2-18 05:20:55
another way:

tempvar c
egen double `c'=count(1), by(sample x_1)
egen double m=mean(x_2), by(sample x_1)
gsort sample -`c' x_1
keep if x_1!=x_1[_n-1]
bysort sample : keep if _n<=5



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群