Filter a table by 2 columns

11 ビュー (過去 30 日間)
Orongo
Orongo 2019 年 2 月 19 日
コメント済み: Ishwor Koirala 2022 年 12 月 21 日
Hi, I'm using 'patients' (load patients) to learn more about tables. I'm trying to filter the table by gender and smoker status by following
load patients
T = table(LastName,Age,Smoker,Gender)
Tsmokermale=(T.Smoker==true| T.Gender=='Male');
but get the error
Undefined operator '==' for input arguments of type 'cell'.
How can I filter the table by male and smoker, and still keep the information of LastName and Age?
  2 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 19 日
please show how you are forming a table
Orongo
Orongo 2019 年 2 月 27 日
T = table(LastName,Age,Smoker,Gender)

サインインしてコメントする。

回答 (2 件)

Peter Perkins
Peter Perkins 2019 年 3 月 11 日
Orongo, do this:
T.Gender = categorical(T.Gender);
Then this
Tsmokermale=(T.Smoker==true| T.Gender=='Male');
works as you expect, and what you are really asking about is this
T(Tsmokermale,:)
Long story short: Gender in the mat file is what's called a cell array of char vectors (a.k.a. a "cellstr"). Those don't support testing using ==, thus KSSV's suggestion of strcmp. But if you convert them to categorical, you can use ==. You could also convert to string, but categorical is more appropriate in this case.
  1 件のコメント
Ishwor Koirala
Ishwor Koirala 2022 年 12 月 21 日
That worked for me. Thank you very much :)

サインインしてコメントする。


KSSV
KSSV 2019 年 2 月 19 日
idx = strcmp(T.Gender,'Male') ;
  1 件のコメント
Orongo
Orongo 2019 年 2 月 27 日
編集済み: madhan ravi 2019 年 2 月 27 日
This works fine for filtering the gender, just what I want. However, I'm missing the information about the row data. So, in my table below with heading {LastName, Age, Smoker, Gender} I get answer 1s and 0s with the command, but I want the last name, age and gender to filter through too.
'Smith' 38 1 'Male'
'Johnson' 43 0 'Male'
'Williams' 38 0 'Female'
'Jones' 40 0 'Female'
'Brown' 49 0 'Female'
'Davis' 46 0 'Female'
'Miller' 33 1 'Female'
'Wilson' 40 0 'Male'
'Moore' 28 0 'Male'
'Taylor' 31 0 'Female'
'Anderson' 45 0 'Female'
'Thomas' 42 0 'Female'
'Jackson' 25 0 'Male'
'White' 39 1 'Male'
'Harris' 36 0 'Female'
'Martin' 48 1 'Male'
'Thompson' 32 1 'Male'
'Garcia' 27 1 'Female'
'Martinez' 37 0 'Male'
'Robinson' 50 0 'Male'
'Clark' 48 0 'Female'
'Rodriguez' 39 0 'Female'
'Lewis' 41 0 'Female'
'Lee' 44 1 'Female'
'Walker' 28 1 'Female'
'Hall' 25 0 'Male'
'Allen' 39 0 'Female'
'Young' 25 0 'Female'
'Hernandez' 36 0 'Male'
'King' 30 1 'Male'
'Wright' 45 1 'Female'
'Lopez' 40 0 'Female'
'Hill' 25 0 'Female'
'Scott' 47 0 'Male'
'Green' 44 0 'Male'
'Adams' 48 0 'Female'
'Baker' 44 1 'Male'
'Gonzalez' 35 0 'Female'
'Nelson' 33 1 'Male'
'Carter' 38 0 'Female'
'Mitchell' 39 1 'Male'
'Perez' 44 0 'Male'
'Roberts' 44 1 'Male'
'Turner' 37 1 'Male'
'Phillips' 45 0 'Male'
'Campbell' 37 0 'Female'
'Parker' 30 0 'Male'
'Evans' 39 0 'Female'
'Edwards' 42 0 'Male'
'Collins' 42 1 'Male'
'Stewart' 49 1 'Male'
'Sanchez' 44 1 'Female'
'Morris' 43 1 'Female'
'Rogers' 47 0 'Female'
'Reed' 50 1 'Male'
'Cook' 38 0 'Female'
'Morgan' 41 0 'Female'
'Bell' 45 1 'Male'
'Murphy' 36 0 'Male'
'Bailey' 38 0 'Female'
'Rivera' 29 0 'Female'
'Cooper' 28 0 'Female'
'Richardson' 30 0 'Female'
'Cox' 28 0 'Female'
'Howard' 29 0 'Female'
'Ward' 36 0 'Male'
'Torres' 45 0 'Female'
'Peterson' 32 0 'Female'
'Gray' 31 0 'Female'
'Ramirez' 48 1 'Female'
'James' 25 0 'Male'
'Watson' 40 1 'Female'
'Brooks' 39 0 'Male'
'Kelly' 41 0 'Female'
'Sanders' 33 1 'Female'
'Price' 31 1 'Male'
'Bennett' 35 0 'Female'
'Wood' 32 0 'Male'
'Barnes' 42 0 'Male'
'Ross' 48 0 'Female'
'Henderson' 34 0 'Male'
'Coleman' 39 0 'Male'
'Jenkins' 28 1 'Male'
'Perry' 29 0 'Female'
'Powell' 32 0 'Female'
'Long' 39 1 'Male'
'Patterson' 37 1 'Female'
'Hughes' 49 1 'Female'
'Flores' 31 1 'Female'
'Washington' 37 0 'Female'
'Butler' 38 1 'Male'
'Simmons' 45 0 'Male'
'Foster' 30 0 'Female'
'Gonzales' 48 0 'Male'
'Bryant' 48 0 'Female'
'Alexander' 25 1 'Male'
'Russell' 44 1 'Male'
'Griffin' 49 0 'Male'
'Diaz' 45 1 'Male'
'Hayes' 48 0 'Male'

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by