Splitapply and unique returning nonscalar value in the same table?
4 views (last 30 days)
Show older comments
Flynn McGuire
on 22 Jun 2020
Edited: Flynn McGuire
on 22 Jun 2020
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
Accepted Answer
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).'}
More Answers (0)
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!