楼主: danccyf
1896 3

[面板数据求助] [数据处理]stata新人想了好久都想不出来。。求助 [推广有奖]

  • 1关注
  • 0粉丝

大专生

21%

还不是VIP/贵宾

-

威望
0
论坛币
111 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
2461 点
帖子
23
精华
0
在线时间
57 小时
注册时间
2011-2-17
最后登录
2021-11-30

楼主
danccyf 发表于 2014-2-18 02:37:02 |AI写论文
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
关键词:Stata 数据处理 tata Sample AMPL 平均值

本帖被以下文库推荐

沙发
hplcdadong 发表于 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

板凳
jjjj6666 发表于 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



您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-28 13:55