Code covered by the BSD License  

Highlights from
mergecellkey: Merge cell by key

image thumbnail

mergecellkey: Merge cell by key

by

 

mergecellkey: Merge cell by key

mergecellkey(A, B, keys)
function C = mergecellkey(A, B, keys)
% A is cell for econometirc estimation
% trans string to number
if size(keys,1) == 1
    keys = keys';
end

headA = A(1,:);
headB = B(1,:);
headAB = union(headA, headB);
nvar = size(headAB, 2);
headABi = cell(nvar, 1);
for ii = 1:nvar
    headABi{ii} = ['v' num2str(ii)];
end % ii
% headABi = strcat('v',  num2cell(num2str([1:nvar]')) );
map1 = [headABi, headAB' ];
map2 = [headAB', headABi];

headAi = repcmpRobust(headA, map1);
headBi = repcmpRobust(headB, map1);

A(1,:) = headAi;
B(1,:) = headBi;

% uuheadBi = unique(headBi)
% uuheadB = unique(headB)
% iuheadBi = idrepetition(headBi)
% showrepetition(A)
% Ads = cell2ds(A);
% Bds = cell2ds(B);
Ads = cell2dataset(A, 'ReadVarNames', true);
Bds = cell2dataset(B, 'ReadVarNames', true );

keystmp = repcmp(keys, map1);
[Cds]  =  join(Ads,Bds,'Keys', keystmp' ,'Type','outer',...
   'MergeKeys',true);
C = ds2cell(Cds);

% deal with _left and _right

cc = 2;

headCi = C(1,:);
for ii =1:size(C, 2)
    nname = size(C, 2);
    if strfind(headCi{ii}, '_left')
        nameSimp = headCi{ii}(1, 1:end-5);
        for jj = 1:size(headCi, 2)
           if strfind(headCi{jj}, '_right')
               if strcmpi(nameSimp, headCi{jj}(1, 1:end-6))
%                    fprintf('match left  %s  to right %s \n', nameSimp, headCi{jj}(1, 1:end-6));
                   for kk = 2:size(C, 1)
                       % rewrite left
                       if ischar(C{kk, ii}) %&& ~strcmp(C{kk, ii},C{kk, jj})) || C{kk, ii}~=C{kk, jj}
                           sigEmptyleft = isempty(C{kk, ii}) | strcmpi(C{kk, ii}, '') | strcmpi(C{kk, ii}, ' ') | strcmpi(C{kk, ii}, '  ');
                           sigEmptyright = isempty(C{kk, jj}) | strcmpi(C{kk, jj}, '') | strcmpi(C{kk, jj}, ' ') | strcmpi(C{kk, jj}, '  ');
                           if sigEmptyleft
                               C{kk, ii} = C{kk, jj};
                           elseif ~( sigEmptyleft) & ~( sigEmptyright) & ~strcmp(C{kk, ii},C{kk, jj}) 
                                   headconflict =  repcmp({nameSimp}, map2);
                                   cellconflict(cc,:) = {kk ,ii, C{kk,1}, headconflict{1,1},  C{kk,ii}, C{kk,jj}} ;
                                   cc = cc+1;                 
                           end
                       else % the number cols
                           if isnan(C{kk, ii}) || isempty(C{kk, ii}) || strcmpi(C{kk, ii}, ' ')
                               C{kk, ii} = C{kk, jj};
                           elseif ~(isnan(C{kk, jj}) || isempty(C{kk, jj}) || strcmpi(C{kk, jj}, ' ')) && C{kk, ii}~=C{kk, jj}
                                   headconflict =  repcmp({nameSimp}, map2);
                                   cellconflict(cc,:) = {kk ,ii, C{kk,1},headconflict{1,1},  C{kk,ii}, C{kk,jj}} ;
                                   cc = cc+1;                 
                           end
                      end
                       % del right
                   end
               end
           end % if strfind
        end % for jj
    end
end % for ii
if cc>2
    cellconflict(1,:) = {'row', 'col', 'keyvar', 'Varname', 'Left', 'right'};
    showcell(cellconflict);
end
% cellconflict
for jj = size(C, 2):-1:1
   if strfind(C{1, jj}, '_left')
       C{1, jj} = C{1, jj}(1, 1:end-5);
   end
   if strfind(C{1, jj}, '_right')
       C = [C(:, 1:jj-1) C(:, jj+1:end)];
   end
   
end

% chage name back to normal form
headCi = C(1,:);
headC = repcmp(headCi, map2);
C(1,:) = headC;

return;

end

Contact us