grpstats for several variables

Hello everybody,
i have values for Sodium, creatinine and many more variables for patients. I need to calculate mean median values of them. Should i use everytime mean(ds.Sodium) or is there a easier way to do that ? i ve been trying to use grpstats function i couldnt make it work. if someone would explain me i would be very happy. thank you

2 Comments

What problems are you having? There are lots of examples in documentation which is the first place to go for help.
Ongun Palaoglu's answer moved here as a comment.
True,
DSSTATS = grpstats(DS,GROUPVARS,WHICHSTATS) .
stat1 = grpstats(ds, {'Sodyum' , 'AST'} , {'mean' , 'median'}) i did this, for my dataset ds, {variables} , {what i want to calculate'means'}

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 9 Sep 2019
Edited: Adam Danz on 9 Sep 2019
Have a look at the list of statistics that are accepted as character vectors. Note that 'median' is not one of the options.
Instead, you'll need to use a function handle to list 'median' as a stat.
stat1 = grpstats(ds, {'Sodyum', 'AST'} , {'mean', @median});
That fixes any syntax issues. If problesm persist, please describe the problem in detail and share the result of the following line (assuming ds is a table which appears to be the case).
head(ds) % if ds is a table

6 Comments

Adam Danz
Adam Danz on 9 Sep 2019
Edited: Adam Danz on 9 Sep 2019
@ Ongun Palaoglu , please use comment sections to continue discussion unless you're posting an answer. It makes it much easier to follow the threads.
Ongun Palaoglu's answer moved here as a comment, below
ds is dataset
Error using dsgrpstats (line 262)
Data variables must be numeric or logical.
Error in grpstats (line 136)
[varargout{1:nargout}] = dsgrpstats(x,group,whichstats,varargin{:});
these are the errors i have.
Datasets might be removed in future releases according to the documentation.
You may want to re-write your code so that your data are stored in tables instead of datasets. You could also convert your dataset to a table using dataset2table().
Conserning this error, "Data variables must be numeric or logical.", I'd like to see what you've got stored in your dataset. This line below will show the first 5 lines (you could add more if needed), assuming your dataset is named "ds". Could you show us this sample?
ds(1:5,:)
Or, better yet, convert your dataset to a table and show us,
head(dsTable)
@ Ongun Palaoglu, please use the comments section for discussion rather than posting comments as answer.
I looked at your data and it will be very easy to follow the advice I provided above.
Frist convert your dataset to a table.
T = dataset2table(ds1);
Because your table contains columns that are non-numeric, you need to specify which columns should be used for the statistics. To do that, use the DataVars property in grpstats(). I listed "AST" and "ALT" but you can replace that with whatever numeric columns you'd like to use for the statistics.
stat1 = grpstats(t, {'Sodyum', 'AST'} , {'mean', @median},'DataVars',{'AST','ALT'})
% --> here ^^^^^^^^^^^^^^^^^^^^^^
About recommending a book, I actually don't recommend a book. Whenever you use a new function, look up that function in the documentation and read about it's inputs, outputs, and what the function does. Also look at it's optional parameters. This is the best way to learn how to use functions.
Ongun Palaoglu
Ongun Palaoglu on 11 Sep 2019
Edited: Ongun Palaoglu on 11 Sep 2019
I have one more question. When I say 'mean' there should be one value and also for 'median', but there are a lot of values in here. Why is that.
Thank you very much for your time and help. I really appreciate.
Ongun, please spend some time reading through the documentation. I provided a link to the grpstats() function. Often times it help looking at the examples too. Look for an example in the grpstats page that matches your inputs.
In this line
stat1 = grpstats(T, {'Sodyum', 'AST'} , {'mean', @median},'DataVars',{'ALT'})
you are computing the mean and median ALT for each "Sodym" and "AST" group -combination. Those group combinations are the row names in the table output.
okay thank you=)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!