"Data variables must be numeric or logical" despite variable being logical when using grpstats

8 views (last 30 days)
function ViewData(data)
resp = cell2mat(data(:, 3)) == 39;
corrAns = ismember(data(:, 1), {'2F_H', '2F_V'});
score = double(corrAns == resp);
ifi = cell2mat(data(:, 2));
orient2 = ismember(data(:,1), {'1F_H', '2F_H'});
tblData = table(score, ifi, data(:, 1), orient2, 'variablenames', {'score', 'ifi', 'trialType', 'orient2'});
% tblData = table(score, ifi, orient, data(:, 1), 'variablenames', {'score', 'ifi', 'trialType'});
tblMean = grpstats(tblData, {'orient2', 'ifi'}); %change trialType to orient, or vice versa
disp(tblMean);
end
So the error is "Error using dsgrpstats (line 293) Data variables must be numeric or logical."
But I'm confused because orient2 is literally a logical, no? If I define tblMean in this way, however, it works:
tblData = table(score, ifi, data(:, 1), orient2, 'variablenames', {'score', 'ifi', 'trialType', 'orient2'});
% tblData = table(score, ifi, orient, data(:, 1), 'variablenames', {'score', 'ifi', 'trialType'});
tblMean = grpstats(tblData, {'trialType','orient2', 'ifi'});
and the column for orient2 LITERALLY is "true" or "false" all the way down. AND when I do class(tblData.trialType), it's "cell" which adds even more to the confusion.
I'm sure it's a simple fix but I can't see it. Thank you!

Accepted Answer

Cris LaPierre
Cris LaPierre on 12 Jan 2022
Edited: Cris LaPierre on 12 Jan 2022
The issue is not your grouping variable, it is your tblData. Specifically, trialType, which is a cell, and not numeric or logical. Here's a simple example using a built-in data set with your names.
load('patients.mat')
orient2 = ismember(Gender,{'Male'});
tblData = table(Age,Diastolic,Systolic,orient2,'VariableNames',{'score', 'ifi', 'trialType', 'orient2'});
% All table variables are numeric or logical
tblMean = grpstats(tblData, {'orient2', 'ifi'})
tblMean = 41×5 table
orient2 ifi GroupCount mean_score mean_trialType _______ ___ __________ __________ ______________ 0_68 false 68 1 42 115 0_70 false 70 1 46 121 0_73 false 73 2 38 122 0_74 false 74 2 39.5 120 0_75 false 75 3 41 119.67 0_76 false 76 3 30.667 121.67 0_77 false 77 3 37 117.33 0_78 false 78 1 41 117 0_79 false 79 7 37.714 119.43 0_80 false 80 6 33.833 119.5 0_81 false 81 2 37.5 117.5 0_82 false 82 2 40 124 0_83 false 83 3 42 124.33 0_85 false 85 3 32.667 120.67 0_86 false 86 2 39 117.5 0_87 false 87 1 31 123
% Table variable Gender (renamed to trialType) is a cell
tblData = table(Age,Diastolic,Gender,orient2,'VariableNames',{'score', 'ifi', 'trialType', 'orient2'});
tblMean = grpstats(tblData, {'orient2', 'ifi'})
Error using dsgrpstats (line 293)
Data variables must be numeric or logical.

Error in grpstats (line 144)
[varargout{1:nargout}] = dsgrpstats(x,group,whichstats,varargin{:});
Use the 'DataVars' name-value pair in the input to specify which numeric or logical variables to compute stats for.
Your second approach works because the cell data is now a grouping variable, so stats are not calculated for it. Perhaps you want to do something like this?
function ViewData(data)
resp = cell2mat(data(:, 3)) == 39;
corrAns = ismember(data(:, 1), {'2F_H', '2F_V'});
score = double(corrAns == resp);
ifi = cell2mat(data(:, 2));
orient2 = ismember(data(:,1), {'1F_H', '2F_H'});
tblData = table(score, ifi, data(:, 1), orient2, 'variablenames', {'score', 'ifi', 'trialType', 'orient2'});
tblMean = grpstats(tblData, {'orient2', 'ifi'},'mean','DataVars','score')
end
  2 Comments
Gabriel Wooten-Soto
Gabriel Wooten-Soto on 18 Jan 2022
Thank you so much! so in my second attempt, how come trialType functions as a grouping variable and orient2 doesn't? Is it because it's a cell?
Cris LaPierre
Cris LaPierre on 18 Jan 2022
In your second attempt, you have specified 3 grouping variables. All three are used. The data is first grouped by 'trialType', then, within each of those groups, by 'orient2', and finally within each of those groups, by 'ifi'.
Using the same example from above, you can see that in the output below.
  1. The data is first grouped by 'trialType'. {'Male'} first, them {'Female'}
  2. Within each of those groups, the data is grouped by 'true' and 'false'
  3. Within each of those groups, the data is grouped by 'ifi'.
The GroupCount column shows you how many items are in the specified grouping combination. So in the first row, for the group trialType=Male, orient2=true and ifi=74 there were 3 scores averaged together.
load('patients.mat')
orient2 = ismember(Gender,{'Male'});
tblData = table(Age,Diastolic,Systolic,orient2,'VariableNames',{'score', 'ifi', 'trialType', 'orient2'});
% Table variable Gender (renamed to trialType) is a cell
tblData = table(Age,Diastolic,Gender,orient2,'VariableNames',{'score', 'ifi', 'trialType', 'orient2'});
tblMean = grpstats(tblData, {'trialType','orient2', 'ifi'});
head(tblMean)
ans = 8×5 table
trialType orient2 ifi GroupCount mean_score _________ _______ ___ __________ __________ Male_1_74 {'Male'} true 74 3 37.667 Male_1_75 {'Male'} true 75 1 25 Male_1_76 {'Male'} true 76 2 43 Male_1_77 {'Male'} true 77 4 37.5 Male_1_78 {'Male'} true 78 3 34 Male_1_79 {'Male'} true 79 2 43.5 Male_1_80 {'Male'} true 80 2 40 Male_1_81 {'Male'} true 81 2 36
tail(tblMean)
ans = 8×5 table
trialType orient2 ifi GroupCount mean_score __________ _______ ___ __________ __________ Female_0_85 {'Female'} false 85 3 32.667 Female_0_86 {'Female'} false 86 2 39 Female_0_87 {'Female'} false 87 1 31 Female_0_88 {'Female'} false 88 2 37 Female_0_90 {'Female'} false 90 1 44 Female_0_91 {'Female'} false 91 4 39.25 Female_0_92 {'Female'} false 92 2 41 Female_0_96 {'Female'} false 96 2 38.5

Sign in to comment.

More Answers (0)

Categories

Find more on Tables 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!