Calculate mean of all variables that have a mean
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marcus Glover
el 14 de Jun. de 2024
Comentada: Paul
el 25 de Jun. de 2024
I have a table with mixed types of variables. I want to calculate the mean of any variable that can have a mean without converting to another type- so just numericals, logicals, etc. and leave string types alone. I do not want to specify which ones to find the mean for because I will probably add variables to this table eventually. The AI made up this apparently fake option, which is what I am looking for: 'Exclude', {'StringVar'}
0 comentarios
Respuesta aceptada
Steven Lord
el 14 de Jun. de 2024
Instead of calling varfun twice as the answer posted by @Arun does, call it once with the InputVariables name-value argument. The value of InputVariables can be a function handle that varfun can execute on the table variables; if the function handle returns true varfun will process that variable.
load patients
T = table(LastName,Age,Height,Weight);
T.Over40 = T.Age > 40;
head(T)
T2 = varfun(@mean, T, InputVariables=@(x) isnumeric(x) | islogical(x))
There is no variable mean_LastName in T2 because the LastName variable in T did not satisfy isnumeric | islogical.
6 comentarios
Marcus Glover
el 25 de Jun. de 2024
Editada: Marcus Glover
el 25 de Jun. de 2024
Paul
el 25 de Jun. de 2024
Interesting. The doc page Mode - Input Argument does not explicitly list char as an acceptable class for the input array. But the doc page Mode - Output Arguments does say that "If the input A is an array, then the output M is an array of the same class."
On the other hand, the doc page for mean is silent on the list of acceptable input arguments, but the discussion under the 'outtype' argument clearly allows for char input. But that section is kind of hard to follow for char input. I guess one could arguy that from that section that char input should result in a double output.
Seems odd that mean and mode would return different class of output for the same input.
Más respuestas (1)
Arun
el 14 de Jun. de 2024
Editada: Arun
el 14 de Jun. de 2024
I understand that you wish to calculate the mean of any variable in a table without specifying which ones to target specifically.
To achieve this for variables that naturally support mean calculation (such as numerical and logical types) without needing to explicitly convert types or specify variables, one can programmatically determine the variable types and then calculate the mean for those that are appropriate. This method is flexible and will automatically adjust to new variables added to the table.
Following are the steps that can be followed to achieve the required objective:
- Step-1: Identify Numerical and Logical Variables: Use the table's “vartype” method or inspect variable types with “varfun” to identify variables that are either numerical or logical, as these are the types for which a mean would typically be meaningful.
- Step-2: Calculate Mean for Identified Variables:Use the identified variable names to calculate their means.
Here is an example script that demonstrates this approach:
% Example table with mixed variable types
T = table([1; 2; 3; 4], {'a'; 'b'; 'c'; 'd'}, [true; false; true; false], 'VariableNames', {'NumericVar', 'StringVar', 'LogicalVar'});
% Identify variables that are either numerical or logical
isNumericOrLogical = varfun(@(x) isnumeric(x) || islogical(x), T, 'OutputFormat', 'uniform');
% Calculate mean for numerical and logical variables only
means = varfun(@mean, T(:, isNumericOrLogical), 'OutputFormat', 'table');
% Display the result
disp(means);
This method avoids the need for hard-coding variable names or types and will automatically adapt as you add or remove variables from the table, provided one rerun the script or incorporate it into a function that operates on your table.
Please refer the following documentation links for more information regarding the related functions:
- vartype: https://www.mathworks.com/help/matlab/ref/vartype.html
- varfun: https://www.mathworks.com/help/matlab/ref/table.varfun.html
Hope this helps!
Regards
Arun
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!