Return rows from a table with a unique combination of categories

1 次查看(过去 30 天)
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 个评论
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