How to merge two tables, keeping independent variable (x) the same but summing the dependent variable (y)
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
KSSV
2021년 9월 15일
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)
추가 답변 (1개)
Stephen23
2021년 9월 21일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!