Performing count in column given a condition specified in a different column?

I have formed a table after doing some data cleaning. It contains two columns, one which has a score of 1 or 0 and one which specifies which question type was being answered (i.e. 'MAY' or 'DG'). I want to add up the total for 'MAY' and 'DG' separately, but have run into trouble.
Was planning to use an equivalency (as here https://uk.mathworks.com/matlabcentral/answers/questions/new/?s_tid=gn_mlc_ans_ask) but when converting to matrix using cell2mat I receive this error
```Error using cell2mat (line 42)
Subscripting a table using linear indexing (one subscript) or
multidimensional indexing (three or more subscripts) is not supported. Use
a row subscript and a variable subscript.```
Any support would be much appreciated

 采纳的回答

Use more straightforward builtin functions for tables/categorical variables...
>> Y=[repmat({'MAY'},8,1);repmat({'DG'},5,1)]; % build a suitable dummy table...
>> Y=Y(randperm(numel(Y)))
>> Z=randi(2,numel(Y),1)-1
>> t=table(categorical(Y),Z,'VariableNames',{'Y','Z'})
t =
13×2 table
Y Z
___ _
MAY 0
MAY 1
DG 0
MAY 0
DG 1
MAY 1
MAY 0
MAY 0
MAY 0
MAY 1
DG 1
DG 0
DG 1
>>
Can see what overall is...
>> summary(t) % just for display...
Variables:
Y: 13×1 categorical
Values:
DG 5
MAY 8
Z: 13×1 double
Values:
Min 0
Median 0
Max 1
>>
Now to answer the Q?
>> countcats(t.Y)
ans =
5
8
>> categories(t.Y)
ans =
2×1 cell array
{'DG' }
{'MAY'}
>>
Also look at
doc splitarray
doc findgroups
ADDENDUM Thanks to Matt J; forgot about varfun
>> varfun(@sum,t,'GroupingVariables','Y')
ans =
2×3 table
Y GroupCount sum_Z
___ __________ _____
DG 5 3
MAY 8 3
>>

2 个评论

Seriously appreciative of your time. Thanks so much for the help. Went with the varfun function as it seemd easier
Hi DPB,
As I realised I need to do a t-test comparing the mean of the two groups I need to preserve the original data (rather than add it as with varfun) but split it into two columns one for 'MAY and one for 'DG'. Can Varfun be used for this, or should I use your original code. I did just try and use your original code but had come trouble getting it to work. I feel like it isn't pulling data from the specific table I need to (entitled 'mc_DG') how would I specify to make sure it takes the data from this table. Sorry if this is stupid but just started with MATLAB and having some trouble.

请先登录,再进行评论。

更多回答(1 个)

varfun(@sum,yourTable,'GroupingVariables',1)

3 个评论

Hi Matt, I've just realised that as I need to do a t-test comparing the mean of the two groups that I need to preserve the original data but split it into two columns (in reality there are about 500 data pices for each group 'MAY' and 'DG' rather than the few I showed in my pic). Is it possible to use varfun to separate the groups data rather than separate and add them as seems to happen when using your code. Sorry if this question is stupid. Just beginning with MATLAB and finding it quite challenging.
You can do,
T=varfun(@(c) {c}, yourTable,'GroupingVariables',1);

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by