Clear Filters
Clear Filters

Splitapply and unique returning nonscalar value in the same table?

3 views (last 30 days)
Hello Matlab Gods,
I'm new to this so I'm gonna post all my code.
data = mycsvfile;
coled = data(:,'ED');
colicu = data(:,'ICU');
ed_patients = unique(data.mrn(table2array(coled) == 1));
ednonicu = unique(data.mrn(table2array(coled) == 1 & table2array(colicu) == 0));
edicu = unique(data.mrn(table2array(coled) == 1 & table2array(colicu) == 1));
% this works no problem:
demographicvarnames = {'age', 'sex', 'race', 'homeless', 'ethnicity','facility'};
ednonicu_data = data(ismember(data.mrn,ednonicu),:);
ed_nonicu_demographicvarnames = ednonicu_data(:, demographicvarnames);
G = findgroups(ednonicu_data.mrn);
all_summary_ed_nonicu_demographicvarnames = struct();
for i = 1:length(demographicvarnames)
varname = demographicvarnames{:, i};
all_summary_ed_nonicu_demographicvarnames.(varname) = splitapply(@unique, ed_nonicu_demographicvarnames(:,varname), G);
end
%this returns the error
ed_nonicu_baseline = {'Current_Smoker','Former_Smoker','Smoking_packyears','IVDU','History__of_receiving_annual_flu_vaccine','A1c','BMI','Heighty','Ejection_Fraction','PFTs_FEV1FVC_Ratio','CD4_count','Viral_Load','Elevated_LDL','Elevated_Triglycerides','Baseline_LFTs_ALT','Baseline_LFTs_AST','Iron_Level','Total_Iron_Binding_Capacity','Serum_Ferritin','Transferrin_Saturation'};
ednonicu_data = data(ismember(data.mrn,ednonicu),:);
ed_nonicu_baseline_data = ednonicu_data(:, ed_nonicu_baseline);
G = findgroups(ednonicu_data.mrn);
all_summary_ed_nonicu_baseline_data = struct();
for i = 1:length(ed_nonicu_baseline)
varname = ed_nonicu_baseline{:, i};
all_summary_ed_nonicu_baseline_data.(varname) = splitapply(@unique, ed_nonicu_baseline_data(:,varname), G);
end
Error using splitapply (line 132)
The function 'unique' returned a non-scalar value when applied to the 1st group of data.
All of the variables are from the same table. there's a mix of categorical/doubles with <undefined> and nan.
This doesn't seem to bother the first bit. Only the second. I can't see a difference between them.
Please help and lemme know if you need further info!
  2 Comments
Flynn McGuire
Flynn McGuire on 22 Jun 2020
Unfortunately, I don't think I can.
I can tell you this?
demographicvarnames = {'age' %double% , 'sex' %categorical%, 'race' %categorical%, 'homeless' %categorical%, 'ethnicity' %categorical%,'facility' %categorical%};
ed_nonicu_baseline = {'Current_Smoker','Former_Smoker','Smoking_packyears','IVDU','History__of_receiving_annual_flu_vaccine', %Categoricals% 'A1c','BMI','Heighty','Ejection_Fraction','PFTs_FEV1FVC_Ratio','CD4_count'%doubles%,'Viral_Load' %these are all doubles,'Elevated_LDL','Elevated_Triglycerides'%Categoricals% ,'Baseline_LFTs_ALT','Baseline_LFTs_AST','Iron_Level','Total_Iron_Binding_Capacity','Serum_Ferritin','Transferrin_Saturation'%doubles%};

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Jun 2020
There is no point using @unique if you expect exactly 0 unique results -- you would just use an empty array instead.
There is no point using @unique if you expect exactly 1 unique result -- you would just use the expected result instead.
Therefore you are generally expecting two or more results from the @unique. But can you be sure that the same number of unique values will be returned for each call?
doc splitapply says
If func returns a nonscalar output argument, then the argument must be oriented so that
splitapply can concatenate the output arguments from successive calls to func. For example,
if the input data variables are column vectors, then func must return either a scalar
or a row vector as an output argument.
unique() returns a column vector. Table variables are typically column vectors.
If you are sure that you will return the same number of results from @unique each time, then use @(x) unique(x).' so that you get the row vector. If you are not sure that you will return the same number of results from @unique each time then use @(x) {unique(x)} or @(x) {unique(x).'}
  1 Comment
Flynn McGuire
Flynn McGuire on 22 Jun 2020
Edited: Flynn McGuire on 22 Jun 2020
The same number of unique values completely depends on which patient mrn the loop is going through. What should I use instead?

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!