楼主: 神级天才
7236 8

[数据管理求助] duplicates drop的是那一条观测值 [推广有奖]

  • 13关注
  • 5粉丝

已卖:82份资源

讲师

42%

还不是VIP/贵宾

-

威望
0
论坛币
1227 个
通用积分
14.4906
学术水平
4 点
热心指数
3 点
信用等级
0 点
经验
44176 点
帖子
361
精华
0
在线时间
608 小时
注册时间
2013-12-29
最后登录
2025-9-16

楼主
神级天才 发表于 2019-7-25 10:31:51 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
有个困惑,假设数据如下

duplicates drop year code,force
那么,删除的是哪一条观测值呢? stata 是根据什么来drop的呢?谢谢~


二维码

扫码加我 拉你入群

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

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


回帖推荐

蓝色 发表于4楼  查看完整内容

命令帮助里面也写了 [D] duplicates -- Report, tag, or drop duplicate observations (View complete PDF manual entry) Syntax Drop duplicates duplicates drop duplicates drop varlist , force Description duplicates reports, displays, lists, tags, or drops duplicate observations, depending on the subcommand specified. Duplicates are obser ...

蓝色 发表于3楼  查看完整内容

crup.com.cn/PerCenter/Certificate the duplicates drop command to drop the duplicate observations. The command drops all observations except the first occurrence of each group with duplicate observations. 简单例子测试
纵览天下,独孤求败

沙发
450667569 发表于 2019-7-25 15:29:11
一个code year可以定位到唯一的一个相应数据,如果某code year有n个相同的数据,xtset code year就会显示红字"数据有重复值",duplicates drop就是把相同的n个数据只保留唯一一个。
我个人来说,重复值出现的原因有二:一是数据库本身有重复值;二是公司-年度层面数据是由个体-年度层面数据处理得来。

藤椅
蓝色 发表于 2019-7-25 15:43:00

crup.com.cn/PerCenter/Certificate
the duplicates drop command to drop the duplicate observations.  The command drops all observations except the first occurrence of each group with duplicate observations.


简单例子测试
  1. . clear

  2. . input  id x

  3.             id          x
  4.   1. 1  1
  5.   2. 1  2
  6.   3. 2  3
  7.   4. 2  2
  8.   5. 2  1
  9.   6. end

  10. .
  11. . duplicates report id

  12. Duplicates in terms of id

  13. --------------------------------------
  14.    copies | observations       surplus
  15. ----------+---------------------------
  16.         2 |            2             1
  17.         3 |            3             2
  18. --------------------------------------

  19. . duplicates drop id,force

  20. Duplicates in terms of id

  21. (3 observations deleted)

  22. . list

  23.      +--------+
  24.      | id   x |
  25.      |--------|
  26.   1. |  1   1 |
  27.   2. |  2   3 |
  28.      +--------+
复制代码


板凳
蓝色 发表于 2019-7-25 15:45:40
命令帮助里面也写了

[D] duplicates -- Report, tag, or drop duplicate observations
                  (View complete PDF manual entry)
Syntax

    Drop duplicates
        duplicates drop [if] [in]
        duplicates drop varlist [if] [in] , force

Description

    duplicates reports, displays, lists, tags, or drops duplicate observations,
    depending on the subcommand specified.  Duplicates are observations with
    identical values either on all variables if no varlist is specified or on a
    specified varlist.

    duplicates report produces a table showing observations that occur as one or
    more copies and indicating how many observations are "surplus" in the sense
    that they are the second (third, ...) copy of the first of each group of
    duplicates.

    duplicates tag generates a variable representing the number of duplicates for each observation.  This will be 0 for all unique observations.

    duplicates drop drops all but the first occurrence of each group of duplicated observations.  The word drop may not be abbreviated.


    Any observations that do not satisfy specified if and/or in conditions are
    ignored when you use report, examples, list, or drop.  The variable created
    by tag will have missing values for such observations.

报纸
神级天才 发表于 2019-7-25 17:36:22
duplicates drop drops all but the first occurrence of each group of duplicated observations.  The word drop may not be abbreviated.
谢谢各位!简单用例子 stata试验了下。 一组code 与 year duplicates只保留第一个。

地板
神级天才 发表于 2019-7-25 17:39:40
那么问题来了。假设我的data中,code与year相同的记录,其他var有的空值有的非空,那么 在duplicates drop 后,如何实现 后续var非空最多的记录呢? 也就是最为完整的。  谢谢谢谢! @蓝色

7
塞纳留斯的梦境 发表于 2019-7-26 12:28:51
神级天才 发表于 2019-7-25 17:39
那么问题来了。假设我的data中,code与year相同的记录,其他var有的空值有的非空,那么 在duplicates drop  ...
先把缺失值删掉不就行了

8
神级天才 发表于 2019-7-28 17:01:40
塞纳留斯的梦境 发表于 2019-7-26 12:28
先把缺失值删掉不就行了
有的 不重复的也有空值,还是有用的

9
蓝色 发表于 2019-7-28 18:40:41 来自手机
神级天才 发表于 2019-7-25 17:39
那么问题来了。假设我的data中,code与year相同的记录,其他var有的空值有的非空,那么 在duplicates drop  ...
那就先sort 相应的变量
把有数值的排在前面
然后删除

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

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