How can I merge rows into one with the same ID number?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a spreadsheet with patient IDs and diagnoses; all diagnoses are in their own column and I want to merge the rows based on patient ID to make one row and keep all the data. I'm not sure how to go about doing this?
Example of table:
PT Dx1 Dx2 Dx3
A 1 0 0
A 0 1 0
A 0 0 1
I want:
PT Dx1 Dx2 Dx3
A 1 1 1
0 Commenti
Risposte (1)
Stephen23
il 21 Set 2020
Modificato: Stephen23
il 22 Set 2020
Assuming that the non-diagonal values are exactly zero, then you could use varfun something like this:
>> T = cell2table({'A',1,0,0;'A',0,2,0;'A',0,0,3;'B',4,0,0;'B',0,5,0;'B',0,0,6})
T =
Var1 Var2 Var3 Var4
____ ____ ____ ____
'A' 1 0 0
'A' 0 2 0
'A' 0 0 3
'B' 4 0 0
'B' 0 5 0
'B' 0 0 6
>> U = varfun(@sum,T,'GroupingVariables','Var1')
U =
Var1 GroupCount sum_Var2 sum_Var3 sum_Var4
____ __________ ________ ________ ________
A 'A' 3 1 2 3
B 'B' 3 4 5 6
Of course you can rename/remove those table variables as required.
0 Commenti
Vedere anche
Categorie
Scopri di più su Tables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!