logical indexing within groups

1 回表示 (過去 30 日間)
EM
EM 2017 年 7 月 24 日
編集済み: Andrei Bobrov 2017 年 7 月 24 日
I have a panel data set such that N observations occur over t-time periods. I've created a unique id number for each N-observation which repeats over t-time periods. See photo for example.
Next, I'd like to extract all the individuals such that hsClass==5 & hsClass<5.
Normally a table (table name is panel1) indexing could be achieved by
rows = panel1.hsClass==5 & panel1.hsClass<5;
panel2 = panel1(rows,:);
However, this results in a table with zero rows. In my case I want the indexing to be performed over the grouping variable (G). How can this be done?
  2 件のコメント
per isakson
per isakson 2017 年 7 月 24 日
Andrei Bobrov
Andrei Bobrov 2017 年 7 月 24 日
編集済み: Andrei Bobrov 2017 年 7 月 24 日
Please attach your data as mat file.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 7 月 24 日
[uG, ~, Gidx] = unique(panel1.G);
group_has_a_5 = accumarray(Gidx, panel1.hsclass == 5) >= 1;
row_mask = ismember(panel1.G, uG(group_has_a_5) );
panel2 = panel1(row_mask,:);

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by