How to merge two tables, keeping independent variable (x) the same but summing the dependent variable (y)
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Samantha Farquhar
am 15 Sep. 2021
Beantwortet: Stephen23
am 21 Sep. 2021
Hello,
I am trying to merge and sum two datasets. I have year and catch data from two different locations and would like to combine them into one dataset where year remains constant but catch is summed for the coresponding year.
0 Kommentare
Akzeptierte Antwort
KSSV
am 15 Sep. 2021
T1 = readtable('Confoss_landings.xls') ;
T2 = readtable('Mainefoss_landings.xls') ;
[idx,ia] = ismember(T2.(1),T1.(1)) ;
T = [T1 ;T2] ;
[year,ia,idx] = unique(T.(1),'stable');
xcatch = accumarray(idx,T.(2),[],@nanmean);
iwant = table(year,xcatch)
Weitere Antworten (1)
Stephen23
am 21 Sep. 2021
Simpler:
T1 = readtable('Confoss_landings.xls', 'VariableNamingRule','preserve')
T2 = readtable('Mainefoss_landings.xls', 'VariableNamingRule','preserve')
T3 = outerjoin(T1,T2, 'Keys','year', 'MergeKeys',true);
T3.catch_total = sum([T3.catch_T1,T3.catch_T2],2,'omitnan')
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!