Return rows from a table with a unique combination of categories

7 ビュー (過去 30 日間)
Edward Lannon
Edward Lannon 2020 年 12 月 17 日
コメント済み: Edward Lannon 2020 年 12 月 18 日
Hello,
I have a large table [2166094x9 table] (sample variable attached) and I would like to get the rows from the table that have a unique combination of 6 categorical variables. Location1 and Location2 have combinations that repeatad (e.g., Location1 = LVL1 , Location2 = RV1 represents a duplicate row as Location1 = RV1 Location2 = LVL1) This is what I have, but I can't seem to make it work.
Thank you for time and consideration.
X=[Data.Subnum, Data.Tp, Data.Location1, Data.Location2, Data.Cond, Data.Freq];
j=findgroups(X);
[idx,idy]=unique(j,'stable');
HCUniqueAll=Data(idy,:);
summary(HCUniqueAll)
  1 件のコメント
dpb
dpb 2020 年 12 月 17 日
But,
{Location1, Location2}=[LV1,RV1]
{Location1, Location2}=[RV1,LV1]
are not the same.
It would then appear that if you want them treated the same you should just replace either RV1 or LV1 with the other in either Location1 or Location2. You could do this on creation of the categorical variable with the optional catnames input variable.

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

採用された回答

Eric Sofen
Eric Sofen 2020 年 12 月 17 日
You don't say what's not working, but I think you're probably running into findgroups on an array throwing an error. To find groups across variables, input a table. The rest of your code should work fine on the result.
j = findgroups(Data(:,["Subnum","Tp","Location1","Location2","Cond","Freq"])
  3 件のコメント
dpb
dpb 2020 年 12 月 18 日
I just told you above...
Alternatively, look into ismember
Edward Lannon
Edward Lannon 2020 年 12 月 18 日
Thanks for the help.
This is the monster I created to fix the issue. I think I get the resuls I need though. In the process of double checking
T=Data;
Unique=[]
for i=1:height(unique(categories(T.Subnum)));
Z=T;
catsSubnum=unique(categories(Z.Subnum));
indxSubnum = Z.Subnum==catsSubnum(i);
Zi=Z(indxSubnum,:);
for j=1:height(unique(categories(Zi.Tp)));
catsTp=unique(categories(Zi.Tp));
indxTp = Zi.Tp==catsTp(j);
Zp=Zi(indxTp,:);
for m=1:height(unique(categories(Zp.Cond)));
catsCond=unique(categories(Zp.Cond));
indxCond = Zp.Cond==catsCond(m);
Zm=Zp(indxCond,:);
for b=2:height(unique(categories(Zm.Freq)));
catsFreq=unique(categories(Zm.Freq));
indxFreq = Zm.Freq==catsFreq(b);
Zfinal=Zm(indxFreq,:);
A = [Zfinal.Location1 Zfinal.Location2];
[idx,idx]=unique(sort(A')','rows','stable');
Uniquetemp=Zfinal(idx,:);
Unique = [Unique ; Uniquetemp];
end
end
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by