"Data variables must be numeric or logical" despite variable being logical when using grpstats
5 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Cris LaPierre
2022-1-12
编辑:Cris LaPierre
2022-1-12
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'})
% 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'})
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 个评论
Cris LaPierre
2022-1-18
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.
- The data is first grouped by 'trialType'. {'Male'} first, them {'Female'}
- Within each of those groups, the data is grouped by 'true' and 'false'
- 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)
tail(tblMean)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!