Return rows from a table with a unique combination of categories

4 Ansichten (letzte 30 Tage)
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 Kommentar
dpb
dpb am 17 Dez. 2020
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Eric Sofen
Eric Sofen am 17 Dez. 2020
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 Kommentare
dpb
dpb am 18 Dez. 2020
I just told you above...
Alternatively, look into ismember
Edward Lannon
Edward Lannon am 18 Dez. 2020
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by