How to prevent tabulate function returning frequencies of variables that are not present in the vector?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have the vector v1, with 26 elements, either being 'Live' or 'Non-live'.
When I use tabulate(v1), it returns
tabulate(v1)
Value Count Percent
cat1 0 0.00%
cat2 0 0.00%
Live 5 19.23%
Non-live 21 80.77%
This affects crosstab also. How to prevent tabulate and crosstab functions taking non-existing variables in the vector in to account?
2 comentarios
Stephen23
el 16 de Abr. de 2025
Editada: Stephen23
el 16 de Abr. de 2025
"How to prevent tabulate function returning frequencies of variables that are not present in the vector?"
Most likely they are present in your vector.
The example is easy to replicate by providing TABULATE with a categorical vector containing those four categories:
v1 = categorical(repelem(["Live","Non-live"],[5,21]),["cat1","cat2","Live","Non-live"])
tabulate(v1)
"How to prevent tabulate and crosstab functions taking non-existing variables in the vector in to account?"
Most likely those categories do exist in the data. Therefore the simplest solution is to not create those categories in the vector.
Respuestas (1)
Walter Roberson
el 16 de Abr. de 2025
The problem is that v1 was created as a categorical with additional categories beyond the ones populated.
I do not know of an efficient way to strip off the additional categories. One work-around is
v1cats = categories(v1);
v1stripped = categorical(v1cats(uint32(v1)));
tabulate(v1stripped)
You can adjust the uint32 according to the maximum number of categories the object holds. In this particular case you could use uint8
0 comentarios
Ver también
Categorías
Más información sobre Octave en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!