Return rows from a table with a unique combination of categories

조회 수: 3 (최근 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 CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by