Splitapply and unique returning nonscalar value in the same table?

2 次查看(过去 30 天)
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 个评论
Flynn McGuire
Flynn McGuire 2020-6-22
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%};

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-6-22
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 个评论
Flynn McGuire
Flynn McGuire 2020-6-22
编辑:Flynn McGuire 2020-6-22
The same number of unique values completely depends on which patient mrn the loop is going through. What should I use instead?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by