How to sort a table by category within column

10 次查看(过去 30 天)
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 个评论
phillip kataswa
phillip kataswa 2020-4-6
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 2020-4-6
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-2-1
  2 个评论
Peter Perkins
Peter Perkins 2018-2-3
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 2018-2-6
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by