join tables by categorical variable

3 visualizaciones (últimos 30 días)
cl
cl el 13 de Jul. de 2020
Comentada: Peter Perkins el 24 de Jul. de 2020
I want to join two tables with indices of type categorical. However, I get an error.
I can convert both indices to string, then join, and then convert back to categorical again, but that seems ludicrous.
a = table(categorical(["a", "b", "c"]'), [1:3]')
b = table(categorical(["a", "b", "d"]'), [4:6]')
join(a,b,'Keys','Var1')
Gives "Left and right key variables 'Var1' and 'Var1' have incompatible types."

Respuesta aceptada

cl
cl el 24 de Jul. de 2020
Turns out that my Matlab installation was corrupted, causing the mis-leading error message I received. After re-installation the error message changed and with using innerjoin instead of join on Akira's suggestion the code executed without errors. Thanks.

Más respuestas (1)

Akira Agata
Akira Agata el 14 de Jul. de 2020
Please try innerjoin or outerjoin functions, like:
c1 = innerjoin(a,b,'Keys','Var1');
c2 = outerjoin(a,b,'Keys','Var1','MergeKeys',true);
These outputs are as follows:
>> c1
c1 =
2×3 table
Var1 Var2_a Var2_b
____ ______ ______
a 1 4
b 2 5
>> c2
c2 =
4×3 table
Var1 Var2_a Var2_b
____ ______ ______
a 1 4
b 2 5
c 3 NaN
d NaN 6
  8 comentarios
Steven Lord
Steven Lord el 20 de Jul. de 2020
The problem, I believe, is that the categorical array used for Var1 in a does not have the same categories as the categorical array used for Var1 in b.
categories(a.Var1)
categories(b.Var1)
If you had two categorical arrays, one listing pieces of a house (door, window, chimney) and one listing pieces of a car (door, window, motor) those are both categorical arrays but they're different because each has a category the other doesn't (chimney, motor.)
Peter Perkins
Peter Perkins el 24 de Jul. de 2020
As long as the categoricals are not ordinal, the different categories should not matter. If they are ordinal, the categories must be the same, with the same order.
But in any case, the OP says, "Turns out that my Matlab installation was corrupted, causing the mis-leading error message I received."

Iniciar sesión para comentar.

Categorías

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

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by