楼主: jackyneer
4681 5

[数据管理求助] stata如何分类计数并返回次数最多的值 [推广有奖]

  • 0关注
  • 2粉丝

已卖:495份资源

博士生

26%

还不是VIP/贵宾

-

威望
0
论坛币
7708 个
通用积分
14.6013
学术水平
0 点
热心指数
4 点
信用等级
0 点
经验
22605 点
帖子
99
精华
0
在线时间
343 小时
注册时间
2012-4-8
最后登录
2024-9-26

楼主
jackyneer 发表于 2017-6-2 16:28:25 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
如题,举个栗子:code为001时,a 值出现次数最多的值为1 ,如何生成新的变量b,使得当code为001时b的值为1?求各位大神指教,谢谢。
code    a      b
001    1       1
001    2       1
001    3       1
001    1       1
001    1       1
002    6       6
002     .       6
002     .       6
003    3       3
003    2       3
003    .        3
003    3       3

二维码

扫码加我 拉你入群

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

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

关键词:Stata tata code COD ODE 如何

回帖推荐

逍遥梦蝶 发表于2楼  查看完整内容

Hope this piece of code works. Good luck. However, I would remind you that your original data might be more complicated than what you showed here, so be careful about the identifier of subjects.

黃河泉 发表于4楼  查看完整内容

你要求的就是众数 (mode),试试

沙发
逍遥梦蝶 发表于 2017-6-2 17:20:27
Hope this piece of code works. Good luck. However, I would remind you that your original data might be more complicated than what you showed here, so be careful about the identifier of subjects.
  1. clear all
  2. input str10 code a
  3. code    a      
  4. 001    1      
  5. 001    2      
  6. 001    3      
  7. 001    1      
  8. 001    1      
  9. 002    6      
  10. 002    .      
  11. 002    .      
  12. 003    3      
  13. 003    2      
  14. 003    .        
  15. 003    3      
  16. end

  17. list, sepby(code)

  18. preserve
  19.     drop if a == .

  20.     contract code a
  21.     list, sepby(code)

  22.     bysort code: egen max = max(_freq)
  23.     list, sepby(code)
  24.        
  25.         keep if _freq == max
  26.         rename a a_freqmax
  27.         list, sepby(code)
  28.        
  29.         tempfile maxfreq
  30.         save `maxfreq', replace
  31. restore

  32. merge m:1 (code) using `maxfreq', keepusing(a_freqmax)
  33. list, sepby(code)
  34. drop _merge
  35. rename a_freqmax b
复制代码
已有 1 人评分学术水平 热心指数 收起 理由
夏目贵志 + 5 + 1 热心帮助其他会员

总评分: 学术水平 + 5  热心指数 + 1   查看全部评分

藤椅
jackyneer 发表于 2017-6-2 18:33:36
逍遥梦蝶 发表于 2017-6-2 17:20
Hope this piece of code works. Good luck. However, I would remind you that your original data might  ...
Well thank you very much for helping me solve this problem. Although the realistic condition may a bit more complicated,the method you offered above could always help me to think

板凳
黃河泉 在职认证  发表于 2017-6-4 08:20:23
你要求的就是众数 (mode),试试
  1. * Example generated by -dataex-. To install: ssc install dataex
  2. clear
  3. input float(code a)
  4. 1 1
  5. 1 2
  6. 1 3
  7. 1 1
  8. 1 1
  9. 2 6
  10. 2 .
  11. 2 .
  12. 3 3
  13. 3 2
  14. 3 .
  15. 3 3
  16. end

  17. egen b = mode(a), by(code)
复制代码

报纸
jackyneer 发表于 2017-6-4 10:45:14
黃河泉 发表于 2017-6-4 08:20
你要求的就是众数 (mode),试试
好像也可以,谢谢您热心的解答

地板
jackyneer 发表于 2017-6-4 10:45:19
黃河泉 发表于 2017-6-4 08:20
你要求的就是众数 (mode),试试
好像也可以,谢谢您热心的解答

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

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