Table data grouping, ordering and recording duplicates
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
Hello
I have a table that looks like following:
%
*ID1 ID2 Name Date*
1b11 g121 John 1/1/2015
1b11 g121 Jhn 1/5/2012
1b11 g121 Jn 7/7/2010
1b11 g175 Tom 1/1/2016
1b11 g175 Toom 1/9/2012
1b11 g175 Tm 1/4/2009
1b11 g175 T'm 1/8/1990
1q99 u143 Sara 11/1/2014
1q99 u143 Sra 11/11/1995
1q99 R943 Bob 12/1/2000
I need to change this table to look like the following
%
*ID1 ID2 Name lDate fDate*
1b11 g121 John 1/1/2015 7/7/2010
1b11 g175 Tom 1/1/2016 1/8/1990
1q99 u143 Sara 11/1/2014 11/11/1995
1q99 R943 Bob 12/1/2000 NA
for each ID1, duplicate ID2 rows are removed retaining the most recent NAME, but keeping record of the Last Date (lDate) and First Date (FDate).
Appreciate any help.
Risposte (2)
Peter Perkins
il 16 Nov 2017
0 voti
Most likely, you want to use rowfun with a grouping variable and a function of your own design to reconcile the duplicate rows.
Andrei Bobrov
il 16 Nov 2017
Modificato: Andrei Bobrov
il 16 Nov 2017
vn = {'ID1' 'ID2' 'Name' 'Date'};
c = {'1b11' 'g121' 'John' '1/1/2015'
'1b11' 'g121' 'Jhn' '1/5/2012'
'1b11' 'g121' 'Jn' '7/7/2010'
'1b11' 'g175' 'Tom' '1/1/2016'
'1b11' 'g175' 'Tom' '1/9/2012'
'1b11' 'g175' 'Tm' '1/4/2009'
'1b11' 'g175' 'Tm' '1/8/1990'
'1q99' 'u143' 'Sara' '11/1/2014'
'1q99' 'u143' 'Sra' '11/11/1995'
'1q99' 'R943' 'Bob' '12/1/2000'};
T = cell2table(c,'v',vn);
T_out = rowfun(@fun,T,'G',{'ID1','ID2'},'OutputV',{'Name','lDate','fDate'})
here fun:
function [nm,mx,mn] = fun(x,y)
nm = x(1);
mx = max(y);
mn = min(y);
end
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!