How to merge two tables, keeping independent variable (x) the same but summing the dependent variable (y)
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
KSSV
il 15 Set 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)
Più risposte (1)
Stephen23
il 21 Set 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 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!