How to sort a table by category within column

7 visualizzazioni (ultimi 30 giorni)
I have a table with 11 columns and 59 unnamed rows. I can sort the rows alphabetically, ascending, descending, increasing, decreasing, etc. by using
Sort_Table = sortrows(Table_Name,'variable_name');
How can I sort a table by a category within a column in a way that is not alphabetical? For example, the column is a list of countries (so that several rows have the same country name) and I wish to list first the rows with a certain country that is neither alphabetically first or last. I've tried
Sort_Table = sortrows(Table_Name,'variable_name','variable_value');
as well as
Sort_Table = sortrows(Table_Name,'variable_name',{variable_value});
but neither provided the results I desired. Some help would be appreciated!
  2 Commenti
phillip kataswa
phillip kataswa il 6 Apr 2020
doorsorted = [];
for h = 1:length(door)
if numberOfDoors(h) == "2"
doorsorted = [doorsorted2];
elseif numberOfDoors(h) == "3"
doorsorted = [doorsorted3];
elseif numberOfDoors(h) == "4"
doorsorted = [doorsorted4];
elseif numberOfDoors(h) == "5more"
doorsorted = [doorsorted5];
end
end
what does this code mean
Walter Roberson
Walter Roberson il 6 Apr 2020
numberOfDoors is expected to be a string() array with at least as many entries as the maximum dimension of the variable doors (unless doors is empty on any dimension.)
You start at the beginning of the array numberOfDoors, and compare each entry in turn with the strings "2", "3", "4", or "5more". If it is "2" you replace the entire content of the variable doorsorted with the contents of the variable doorsorted2. If it is "3" instead then you replace the entire content of the variable doorsorted with the contents of the variable doorsorted3, and likewise "4" -> doorsorted4, and "5more" -> doorsorted5. Then you go on to the next entry in numberOfDoors, probably overwriting the entire variable doorsorted when you do.
With all of the overwriting going on, the net effect is:
  • if door is empty, or if none of the entries in numberOfDoors match "2", "3", "4", or "5more", then doorsorted will be left as the empty array door
  • otherwise, find the last entry in numberofDoors that matches one of "2", "3", "4", or "5more", and assign the corresponding variable doorsorted2, doorsorted3, doorsorted4, or doorsorted5 to doorsorted.
The code might well be wrong. I would tend to expect instead
doorsorted = [];
for h = 1:length(door)
if numberOfDoors(h) == "2"
doorsorted(h) = doorsorted2;
elseif numberOfDoors(h) == "3"
doorsorted(h) = doorsorted3;
elseif numberOfDoors(h) == "4"
doorsorted(h) = doorsorted4;
elseif numberOfDoors(h) == "5more"
doorsorted(h) = doorsorted5;
else
doorsorted(h) = nan; %or 0 or inf or "other" or something like that
end
end

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 1 Feb 2018
  2 Commenti
Peter Perkins
Peter Perkins il 3 Feb 2018
Yes, as Walter suggests, you should convert your countries data to a categorical variable, and define whatever order you want them in. You could make the categorical variable ordinal, but even a non-ordinal categorical has a (non-mathematical) ordering that's useful for display and sorting.
Kylie Hansen
Kylie Hansen il 6 Feb 2018
Thank you very much, both of you! I had never of heard of a categorical variable before, but I feel like I can make use of it quite often in the future.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by