Splitapply and unique returning nonscalar value in the same table?

조회 수: 6 (최근 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
Ameer Hamza
Ameer Hamza 2020년 6월 22일
Can you attach the variables used to run this code?
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