我正在尝试让Stata选择每个人(ice_cream
,Amanda
,Christian
)吃掉Paola
的最小值,这样我最终只有3行:
person ice_cream
Amanda 16
Amanda 27
Amanda 29
Amanda 40
Amanda 96
Amanda 97
Christian 19
Christian 23
Christian 26
Christian 27
Christian 28
Christian 34
Christian 62
Christian 70
Christian 78
Paola 5
Paola 11
Paola 28
Paola 97
答案 0 :(得分:4)
避免创建新变量的答案:
sort person ice_cream
by person: keep if _n == 1
答案 1 :(得分:4)
单线解决方案
collapse (min) ice_cream, by(person)
答案 2 :(得分:3)
这应该有效:
* Generate a variable with the group minimums
sort person
by person: egen Min = min(ice_cream)
* Only keep observations with same value as group minimums
keep if Min == ice_cream
* Delete minimum variable
drop Min
注意:这将只留下ice_cream最小值的观察值。如果组中的多个观察值具有ice_cream的最小值,那么您将对该组有多个观察值(请注意,这不在上述数据中,但如果例如ice_cream是因子变量则可能是这样)。如果您想要每组进行一次独特的观察,那么您可以添加:
duplicates drop person, force
答案 3 :(得分:1)
如果您只想显示ice_cream
吃过的最小值
通过Amanda
,Christian
和Paola
,但没有更改数据集,您可以
改为使用summarize
命令:
clear
input str20 person ice_cream
Amanda 16
Amanda 27
Amanda 29
Amanda 40
Amanda 96
Amanda 97
Christian 19
Christian 23
Christian 26
Christian 27
Christian 28
Christian 34
Christian 62
Christian 70
Christian 78
Paola 5
Paola 11
Paola 28
Paola 97
end
bysort person: summarize ice_cream
---------------------------------------------------------------------------
-> person = Amanda
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
ice_cream | 6 50.83333 36.18517 16 97
---------------------------------------------------------------------------
-> person = Christian
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
ice_cream | 9 40.77778 22.63171 19 78
---------------------------------------------------------------------------
-> person = Paola
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
ice_cream | 4 35.25 42.30347 5 97