Applying a function across all columns of a table
18 ビュー (過去 30 日間)
古いコメントを表示
T = readtable('agree'); %%name of table
T.Q1(strcmpi(T.Q1,'Agree')) = {1}; %% change all results in column called Q1 to 1 if agree appears
T.Q1(strcmpi(T.Q1,'Strongly Agree')) = {1}; %% change all results in column called Q1 to 1 if strongly agree appears
T.Q1(strcmpi(T.Q1,'Disagree')) = {0}; %% change all results in column called Q1 to 0 if disagree appears
T.Q1(strcmpi(T.Q1,'Neither Agree Nor Disagree')) = {0};%% change all results in column called Q1 to 0 if neither agree nor disagree appears
T.Q1(strcmpi(T.Q1,'Strongly Disagree')) = {0}; %% change all results in column called Q1 to 0 if strongly disagree appears
T.Q1(strcmpi(T.Q1,"Don't know")) = {0} %% change all results in column called Q1 to 0 if Don't know appears
I have a table of 40 columns which has data from a survey based on a 5 point Likert scale. I want to change the results to binary results, where strongly agree and agree are represented by 1 and everything else is represented by 0. The code above achieves this, but I want to make it into a function which runs it for the whole table and doesnt require me to change the name of the column each time. Could anyone help?
0 件のコメント
採用された回答
Ive J
2020 年 12 月 7 日
編集済み: Ive J
2020 年 12 月 7 日
Working with strings/cell is easier.
T = readtable('agree');
header = T.Properties.VariableNames; % keep table variable names
T = string(T{:, :}); % convert to string
yesIdx = ismember(T, {'Agree', 'Strongly Agree'}); % index of YES answers
T(yesIdx) = 1; T(~yesIdx) = 0; % assign 1/0 values
T = array2table(T, 'VariableNames', header); % convert back to table
I also notice strcmpi in your example above. If you are looking for case insensitive situations, you can further change 4th line to:
yesIdx = ismember(lower(T), {'agree', 'strongly agree'}); % index of YES answers
5 件のコメント
Ive J
2020 年 12 月 7 日
編集済み: Ive J
2020 年 12 月 7 日
Yep, just copy them into a simple function:
function T = char2numAnswerSheet(tabName)
% check input data type if necessary
% blah blah blah
T = readtable(tabName);
header = T.Properties.VariableNames; % keep table variable names
T = string(T{:, :}); % convert to string
yesIdx = ismember(lower(T), {'agree', 'strongly agree'}); % index of YES answers
T(yesIdx) = 1; T(~yesIdx) = 0; % assign 1/0 values
T = array2table(T, 'VariableNames', header); % convert back to table
end % END
その他の回答 (1 件)
Ameer Hamza
2020 年 12 月 7 日
Convert your code into a function like this
function y = myFunc(x)
x(strcmpi(x,'Agree')) = {1}; %% change all results in column called Q1 to 1 if agree appears
x(strcmpi(x,'Strongly Agree')) = {1}; %% change all results in column called Q1 to 1 if strongly agree appears
x(strcmpi(x,'Disagree')) = {0}; %% change all results in column called Q1 to 0 if disagree appears
x(strcmpi(x,'Neither Agree Nor Disagree')) = {0};%% change all results in column called Q1 to 0 if neither agree nor disagree appears
x(strcmpi(x,'Strongly Disagree')) = {0}; %% change all results in column called Q1 to 0 if strongly disagree appears
x(strcmpi(x,"Don't know")) = {0} %% change all results in column called Q1 to 0 if Don't know appears
end
and then try this
T;
T_new = varfun(@myFunc, T)
参考
カテゴリ
Help Center および File Exchange で LaTeX についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!