Select the minimum over blocks of observations

不打扰是莪最后的温柔 提交于 2019-12-11 01:01:52

问题


I am trying to make Stata select the minimum value of ice_cream eaten by every person (Amanda, Christian, Paola) so that I end up with just 3 rows:

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

回答1:


An answer that avoids creating a new variable:

sort person ice_cream
by person: keep if _n == 1



回答2:


A one-line solution

collapse (min) ice_cream, by(person) 



回答3:


This should work:

* 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

Note: This will leave only observations with a minimum value for ice_cream. If multiple observations in a group have the minimum value for ice_cream then you will have multiple observations for that group (Note this is not in the above data but may be likely if for instance ice_cream was a factor variable). If you wanted a unique observation per group you could then add:

duplicates drop person, force



回答4:


If you want to simply display the minimum value of ice_cream eaten by Amanda, Christian and Paola, but without altering your dataset, you can use the summarize command instead:

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


来源:https://stackoverflow.com/questions/36555139/select-the-minimum-over-blocks-of-observations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!