Return rows from a table with a unique combination of categories

1 visualización (últimos 30 días)
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 comentario
dpb
dpb el 17 de Dic. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Eric Sofen
Eric Sofen el 17 de Dic. de 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 comentarios
dpb
dpb el 18 de Dic. de 2020
I just told you above...
Alternatively, look into ismember
Edward Lannon
Edward Lannon el 18 de Dic. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by