MATLAB Answers

0

getting new tables from another

Asked by jean claude on 26 Oct 2018
Latest activity Commented on by Peter Perkins
on 31 Oct 2018
hi, how to get table just for smith; a table for william , and a table for johnson ? imagine i have many rows with different company names and i want to separate them, so getting a table for every company. Here is a simplified example
LastName = {'Smith';'Johnson';'William';'William';'Smith'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Height,Weight,BloodPressure,...
LastName)
output wanted x =
Age Height Weight BloodPressure LastName
___ ______ ______ _____________ ________
38 71 176 124 93 'Smith'
49 64 119 122 80 'Smith'
for william
Age Height Weight BloodPressure LastName
___ ______ ______ _____________ _________
38 64 131 125 83 'William'
40 67 133 117 75 'William'

  2 Comments

Splitting the data up rather defeats the purpose of using a table. Keeping the data all together will mean you can use neat table operations (like splitapply, findgroups, etc.) and will make processing the data easier.
In general splitting data up makes it harder to work with.
Steven Lord
on 26 Oct 2018
I second Stephen's comment. If you had a table with 1000 unique company names, do you really want to create 1000 individual variables in the workspace? That's highly discouraged.
In addition to splitapply and findgroups which Stephen mentioned, there are functions like groupsummary (introduced in release R2018a) and grouptransform (introduced in release R2018b) that may make your workflow with the one larger table easier.
Perhaps if you tell us more about how you're planning to use those small company-specific table arrays we can offer suggestions for how to achieve your goal without creating lots of individual variables.

Sign in to comment.

Products

1 Answer

Answer by madhan ravi
on 26 Oct 2018
Edited by madhan ravi
on 26 Oct 2018

LastName = {'Smith';'Johnson';'William';'William1';'Smith1'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(Age,Height,Weight,BloodPressure,...,
'RowNames', LastName)
smith = T('Smith',:) %creates new table for smith
johnson = T('Johnson',:) %creates new table for Johnson
william = T('William',:) %creates new table for William
COMMAND WINDOW DISPLAYS:
T =
5×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
Johnson 43 69 163 109 77
William 38 64 131 125 83
William1 40 67 133 117 75
Smith1 49 64 119 122 80
smith =
1×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Smith 38 71 176 124 93
johnson =
1×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
Johnson 43 69 163 109 77
william =
1×4 table
Age Height Weight BloodPressure
___ ______ ______ _____________
William 38 64 131 125 83
>>

  6 Comments

yes i know but here i am importing data from excel and it is just a column not rownames, so it can be not unique for this case
madhan ravi
on 26 Oct 2018
mind uploading the excel file with few datas to maniupulate , have to find another way otherwise it's a huge pain
Do a strcmp on the table variable containing the names, and use the logical vector from that as a row subscript on the original table.
But you should considered heeding Stephen and Steve's advice

Sign in to comment.