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.

답변 (2개)

Peter Perkins
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.
Andrei Bobrov
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

이 질문은 마감되었습니다.

질문:

2017년 11월 2일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by