Table data grouping, ordering and recording duplicates
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
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.
0 件のコメント
回答 (2 件)
Peter Perkins
2017 年 11 月 16 日
0 投票
Most likely, you want to use rowfun with a grouping variable and a function of your own design to reconcile the duplicate rows.
0 件のコメント
Andrei Bobrov
2017 年 11 月 16 日
編集済み: Andrei Bobrov
2017 年 11 月 16 日
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
0 件のコメント
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!