MATLAB Answers

Blue
1

How to subset a table with character elements

Asked by Blue
on 9 Aug 2019
Latest activity Answered by Peter Perkins
on 26 Aug 2019
Hi,
I have a very basic question I would like to understand. Lets say we have a basic table like so:
year = {1994, 1994, 1994, 1995, 1995, 1995, 1996, 1996, 1996}.';
type = {'AA', 'BB', 'CC', 'BB', 'BB', 'BB', 'AA', 'CC', 'CC'}.';
t = table(year, type);
Lets further say I want to subset this table by type. I could go with:
j = t(t.type == 'AA',:);
dsMale = hospital(hospital.Sex=='Male',:);
Except this fails: Undefined operator '==' for input arguments of type 'cell'.
The correct answer seems to be this:
k = t(strcmp(t.type, 'AA'), :)
So what gives ? What am I missing ?
Thank you,

  3 Comments

Stephen Cobeldick
2019 年 8 月 9 日
"So what gives ? What am I missing ?"
One thing is that your data is a table. The documentation you linked to is not for tables, it is for a data class that is defined in the Statistics Toolbox called dataset, explained here:
You should be looking at documentation for tables, e.g.:
Another thing is that == only works with string arrays, it does not compare cell arrays of character vectors. For cell arrays of character vectors you will have to use strcmp or similar.
Read about the differences here:
Blue
2019 年 8 月 9 日
Ah. Thank you.
dpb
2019 年 8 月 9 日
"One thing is that your data is a table. The documentation you linked to is not for tables, it is for a data class that is defined in the Statistics Toolbox called dataset"
One of the most maddening things about what TMW has done over the last several years ... they introduce these partially-ready-for-prime-time classes in toolboxes then shortly thereafter essentially abandon them and introduce the same functionality in the main product.
But, with different class definitions, the two aren't interoperational and so confusion reighneth plus the bloat of multiple implementations of the same functionality...it's a mess.
There seems to be nobody in charge of the playpen that prevents such things from being introduced until there is a longterm plan in place...and, once release into the wild, the ultimate loser is just an exotic species left roaming around untended.

Sign in to comment.

Tags

2 Answers

回答者: Samatha Aleti 2019 年 8 月 12 日

For cell arrays, in case of comparison the operator “==” doesn’t help. As you mentioned in the reference document, it worked since, “hospital.Sex” is not of type “cell array” whereas “t.type” in your case is a “cell array”.

  0 Comments

Sign in to comment.


回答者: Peter Perkins
2019 年 8 月 26 日

Blue, I'm a little late to the party, but at least for those data, what you really ought to do is use a categorical for t.type. If you do, t.type == 'AA' absolutely will work.
As others said, string is newer than "cell arrays of char row vectors", and is a much better choice for text, but what you've shown for t.type looks like categorical, and I think you'll be happier storing it that way.

  0 Comments

Sign in to comment.