Indices of the values for which two conditions are true

조회 수: 1 (최근 30일)
Emiliya Taskova
Emiliya Taskova 2020년 5월 11일
댓글: Emiliya Taskova 2020년 5월 11일
I have a table of data where the first column is an abbreviated name (abbrev), second column is the first word of the full name (firstword) and the third is a particular number corresponding to that name. I would like to clean my data from duplicates that have the same 'abbrev' and 'firstword' and sum up the numbers for these duplicates. Some entries may have the same abbreviated name but a different first word- e.g. 'rr' and 'Roger' and 'Dodger' and vice versa, that's why I want to introduce this condition that both the first name and the first word have to match for an entry to be considered a duplicate.
Or in other words from this data:
abbrev =
{'yw' }
{'rr' }
{'yw' }
{'rr' }
firstword =
{'yellow'}
{'Roger' }
{'yellow'}
{'Dodger' }
number =
5
10
1
3
I want to get this:
abbrev =
{'yw' }
{'rr' }
{'rr' }
firstword =
{'yellow'}
{'Roger' }
{'Dodger' }
number =
6
10
3
Thank you in advance!

채택된 답변

Peng Li
Peng Li 2020년 5월 11일
tbl = table(abbrev(:), firstword(:), number(:));
[gp, outTbl] = findgroups(tbl(:, 1:2));
outTbl.sum = splitapply(@sum, tbl.(3), gp)
outTbl =
3×3 table
Var1 Var2 sum
______ __________ ___
{'rr'} {'Dodger'} 3
{'rr'} {'Roger' } 10
{'yw'} {'yellow'} 6
  댓글 수: 1
Emiliya Taskova
Emiliya Taskova 2020년 5월 11일
Peng Li, you're amazing! Thank you so much for the answer!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by