简体   繁体   English

Stata / Loops 删除 15,000 个观测值中的每 9-15 个观测值

[英]Stata / Loops to delete every 9th-15th observation out of 15,000 observations

I am trying to create a loop that deletes every 9th-15th observations out of dataset containing around 15,000 observations.我正在尝试创建一个循环,从包含大约 15,000 个观测值的数据集中删除每 9 到 15 个观测值。 What would be the syntax?语法是什么?

In general you should not loop over observations as there is almost definitely a more efficient and elegant solution, as Nick noted above with the drop if missing(turnout) solution.一般来说,你不应该循环观察,因为几乎肯定有一个更有效和更优雅的解决方案,正如尼克在上面提到的drop if missing(turnout)解决方案。 However, I gather you are a new Stata user so I will run through a hypothetical.然而,我猜你是一个新的 Stata 用户,所以我会假设一下。 Suppose that you in fact did want to drop observations within a certain range rather than based on a secondary variable like turnout .假设您实际上确实希望在某个范围内drop观察,而不是基于像turnout这样的次要变量。 The general strategy could be as follows:一般策略可能如下:

  1. Group observations in groups of 15 (which I think you have here with the id column).以 15 人为一组进行分组观察(我认为您在此处的id列中有)。 In the solution below I will assume you do not have this column.在下面的解决方案中,我将假设您没有此列。
  2. Within each group, drop the observations in the 9th-15th places.在每个组中, drop观察结果放在第 9-15 位。
// Group observations
gen group = ceil(_n/15)
// Within each group, drop in the 9-15 locations 
by group: drop if inrange(_n,9,15)
drop if mod(_n, 9) == 0 

will drop observations 9, 18, 27, and so forth.drop观察值 9、18、27 等。 The same principle applies to any other positive integer.相同的原理适用于任何其他正极 integer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM