Borrar filtros
Borrar filtros

Create new column in a table to be filled based on the other columns

27 visualizaciones (últimos 30 días)
Lavenia
Lavenia el 6 de Feb. de 2024
Comentada: Stephen23 el 6 de Feb. de 2024
Hi! Is there a way i can create a new column called 'Check' to get populated with a number depending on other columns? And from there create another column called 'Test' which determines which patient to test based on the new column 'Check'?
Age = [38;43;38;40;49;56;82;10;15;34;38;12];
Weight = [71;69;64;67;64;60;70;55;80;50;76;98];
Height = [176;163;131;133;119;175;165;148;163;156;174;169]
patient = table(Age, Weight, Height)
patient.BMI = (patient.Weight*0.453592)./(patient.Height*0.0254).^2
%how do i create a new column called 'Check' that gets assigned the numbers 0 to 4, depending on these criteria?
for i = 1:length(patient.BMI)
if patient.BMI(i) > 2.6
Check(i) = 1;
elseif 2.4 < patient.BMI(i) < 2.5 && patient.Height(i) > 150 && patient.Weight(i) > 60
Check(i) = 2;
elseif 2.4 < patient.BMI(i) > 3 && patient.Weight(i) > 80
Check(i) = 3;
elseif (2.4 < patient.BMI(i)) && (patient.BMI(i) < 3) && (patient.Height(i) < 160) && (patient.Weight(i) < 90)
Check(i) = 4;
else
Check(i) = 0;
end
end
%is there a way to add another column called "Test" where if 'Check' is 0, it gets filled with 'No', if 'Check' is 1, then it gets filled with 'No', if 'Check' is 3 then it gets filled with 'Yes' and if 'Check' is 4, it gets filled with 'Maybe'
  1 comentario
Stephen23
Stephen23 el 6 de Feb. de 2024
Editada: Stephen23 el 6 de Feb. de 2024
Before moving on to that column, you should check the rest of your code:
  • What units are WEIGHT in? Look at the values and decide if they are reasonable for those units.
  • What units are HEIGHT in? Look at the values and decide if they are reasonable for those units.
  • What value of BMI do you expect will ever make this TRUE?: (28.00 < patient.BMI(i)) && (patient.BMI(i) < 3). Please tell me one number which is both less than 3 and greater than 28.
  • Note that MATLAB does not have a n-ary logical comparison. The logical comparison operators are all bivariate functions. Repeated comparisons are evaluated from left to right. So your code:
28.00 < patient.BMI(i) < 2.5
is thus equivalent to this:
(28.00 < patient.BMI(i)) < 2.5
which is therefore equivalent to either of these:
true < 2.5
false < 2.5
and thus is trivially (because true=1 and false=0) always
true
In every case where a you write A<B<C you probably want A<B && B<C (and similarly for all other logical comparisons).
Even if MATLAB had chainable logical comparisons, what value is both less than 2.5 and greater than 28?
So you have a few things to fix...then you can move on to generating that text column.

Iniciar sesión para comentar.

Respuestas (1)

Avni Agrawal
Avni Agrawal el 6 de Feb. de 2024
Hi Lavenia,
I understand that you want to fill details into new column based on other columns. You can achieve this by using logical indexing and the `table` function in MATLAB. Below is the code to create the 'Check' column based on the specified criteria and then create the 'Test' column accordingly:
Age = [38;43;38;40;49;56;82;10;15;34;38;12];
Weight = [71;69;64;67;64;60;70;55;80;50;76;98];
Height = [176;163;131;133;119;175;165;148;163;156;174;169];
patient = table(Age, Weight, Height);
% Calculate BMI
patient.BMI = (patient.Weight*0.453592)./(patient.Height*0.0254).^2;
% Create 'Check' column based on criteria
Check = zeros(size(patient,1), 1); % Initialize 'Check' column
for i = 1:size(patient,1)
if patient.BMI(i) > 2.6
Check(i) = 1;
elseif 28.00 < patient.BMI(i) < 2.5 && patient.Height(i) > 150 && patient.Weight(i) > 60
Check(i) = 2;
elseif 28.00 < patient.BMI(i) > 3 && patient.Weight(i) > 80
Check(i) = 3;
elseif (28.00 < patient.BMI(i)) && (patient.BMI(i) < 3) && (patient.Height(i) < 160) && (patient.Weight(i) < 90)
Check(i) = 4;
else
Check(i) = 0;
end
end
% Create 'Test' column based on 'Check' column
Test = cell(size(patient,1), 1); % Initialize 'Test' column
Test(Check == 0 | Check == 1) = {'No'};
Test(Check == 2 | Check == 3) = {'Yes'};
Test(Check == 4) = {'Maybe'};
% Add 'Check' and 'Test' columns to the patient table
patient.Check = Check;
patient.Test = Test;
disp(patient);
This code first calculates the BMI and then iterates over each row to determine the value of the 'Check' column based on the specified criteria. Afterwards, it assigns 'No', 'Yes', or 'Maybe' to the 'Test' column based on the value of the 'Check' column. Finally, both 'Check' and 'Test' columns are added to the 'patient' table.
I hope this helps.
  3 comentarios
Lavenia
Lavenia el 6 de Feb. de 2024
Hi Stephen and Avni,
Sorry i was adapting my very big datafile into a mock dataset so the 28 was a remnant from the original dataset (that is not looking at BMI/weight/height). As i was looking for a way to solve it for my dataset, i found this link to create a new table so i used how they started a table. i have edited it to fix the problem - that is an error on my part.
When i try to plot it, i get an error (sorry im new to matlab so im still trying to understand everything). May i please know what this means? Do i have to download a package/file?
figure
gscatter(patient.Height, patient.Weight, patient.Check)
gscatter(patient.Height, patient.Weight, patient.Test)
Undefined function 'gscatter' for input arguments of type 'double'.
Error in untitled2 (line 34)
gscatter(patient.Height, patient.Weight, patient.Check)
Stephen23
Stephen23 el 6 de Feb. de 2024
"Undefined function 'gscatter' for input arguments of type 'double'."
GSCATTER is in the Statistics and Machine Learning Toolbox:
To use GSCATTER you would need to have that toolbox installed and a valid license for it.

Iniciar sesión para comentar.

Categorías

Más información sobre Manage Products en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by