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!