How can I count specific value in one column but based on another four columns

1 view (last 30 days)
Hello,
How can I count specific value in one colum but based on another four columns
Example
Int_name Country # Milepost lighting condition Frequency
'"A" ST' '06223 15 D' 20.92 'C' ?
'"A" ST' '06223 15 D' 20.92 'C'
'"A" ST' '06145 20 D' 10.09 'A'
'"A" ST' '06145 20 D' 10.09 'A'
I need to count lighting condition: A or C frequency under same milepost under same country number under same Int_name
Like for "A" ST' , country number 06223 15 D and mile post 20.92 we have C ocuured 2 times. and so on for a large data.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 25 Jul 2019
Tout = varfun(@(x)numel(x),T,'g',1:4);
  4 Comments
Guillaume
Guillaume on 25 Jul 2019
Note: from a didactic point of view, it would be much better to give answers that use the full names of Names/Values pair, so learners don't have to work out what 'g' stands for. In this instance:
Tout = varfun(@(x)numel(x),T,'GroupingVariables',1:4);
also the indirection with an anonymous function is not needed:
Tout = varfun(@numel, T, 'GroupingVariables', 1:4)
and of course, the actual names of the variables can be used instead of indices (more reliable in case the order is changed) as long as the variables are correctly spelled.
Adam Danz
Adam Danz on 25 Jul 2019
I agree, Guillaume. I only use full names in all of my codes just in case some day someone will pick it up with less familiarity. Using the full names gives others the chance to look up the meaning of the property.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 25 Jul 2019
Assuming that your dara is in a table (it would be useful if the example used valid matlab syntax):
result = groupsummary(yourtable, {'int_Name', 'Country', 'Milepost'}, @numel, 'lighting_condition')
  7 Comments
Guillaume
Guillaume on 25 Jul 2019
I think I understand. In that case, the simplest is to go with findgroups, build the histogram of that, then redistribute the histogram in the table according to the table
group = findgroups(updateddata1(:, {'int_desc', 'cnty_rte', 'milepost', 'LIGHT'}));
count = accumarray(group, 1); %one of the many ways to get an histogram in matlab. See also histcounts
updateddata1.Frequency = count(group);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!