How to merge two tables, keeping independent variable (x) the same but summing the dependent variable (y)
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Samantha Farquhar
el 15 de Sept. de 2021
Respondida: Stephen23
el 21 de Sept. de 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 comentarios
Respuesta aceptada
KSSV
el 15 de Sept. de 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)
Más respuestas (1)
Stephen23
el 21 de Sept. de 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 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!