How to merge two tables, keeping independent variable (x) the same but summing the dependent variable (y)
8 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
KSSV
on 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)
More Answers (1)
Stephen23
on 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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!