Calculating mean and std from every continuous variable in a table

9 views (last 30 days)
Hello,
I need to write a function that can take a large table containing different types of variables (both categorical and continuous) and return the mean and std of each continuous variable.
Would someone be able to guide me on how to approach this?
Please and thank you !

Accepted Answer

dpb
dpb on 19 Oct 2022
vartype to the rescue...for the base case of numeric
S = vartype('numeric'); % create the indexing variable by type
tStats=[mean(tT{:,S}); std(tT{:,S})]; % compute statistics over content
NOTA BENE: The "curlies" {} braces to dereference the table variables into numeric arrays -- this will work as above IFF all the numeric values are the same class (double, single, ...). If the table consists of multiple numeric types, then to deal with them in one go, you'd want to use convertvars on the numeric subset and turn them all into one class.
Given the nature of a table as being able to hold any data type including numeric as cell content, writing a totally general routine will be fairly detailed undertaking. If your table is simple, then it can be pretty simple as above with only a line or two.
It's unfortunate that there's no equivalent to the Statistics TB grpstats for just returning a set of statistics for a group of variables without the grouping variable being thrown into the mix; you'll have to build that kind of interface to be able to select multiple statistics from a list or just hardcode in those of interest as above....

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!