Diff of /inst/@dataframe/private/df_matassign.m [afaced] .. [591ef1]  Maximize  Restore

Switch to unified view

a/inst/@dataframe/private/df_matassign.m b/inst/@dataframe/private/df_matassign.m
...
...
64
        df._name{1}(indr, :) = []; 
64
        df._name{1}(indr, :) = []; 
65
        df._over{1}(indr) = []; 
65
        df._over{1}(indr) = []; 
66
      endif     
66
      endif     
67
      df._ridx(indr, :, :) = [];
67
      df._ridx(indr, :, :) = [];
68
      %# to remove a line, iterate on each column
68
      %# to remove a line, iterate on each column
69
      df._data = cellfun (@(x) feval(@subsasgn, x, S, []), \
69
      df._data = cellfun (@(x) feval(@subsasgn, x, S, []), ...
70
                          df._data, "UniformOutPut", false);
70
                          df._data, "UniformOutPut", false);
71
      if (isa (indr, 'char'))
71
      if (isa (indr, 'char'))
72
        df._cnt(1) = 0;
72
        df._cnt(1) = 0;
73
      else
73
      else
74
        df._cnt(1) = df._cnt(1) - length (indr);
74
        df._cnt(1) = df._cnt(1) - length (indr);
...
...
119
  
119
  
120
  rname = cell(0, 0); rname_width = max (1, size (df._name{2}, 2)); 
120
  rname = cell(0, 0); rname_width = max (1, size (df._name{2}, 2)); 
121
  ridx = []; cname = rname; ctype = rname;
121
  ridx = []; cname = rname; ctype = rname;
122
  
122
  
123
  if (iscell (RHS))
123
  if (iscell (RHS))
124
    if ((length (indc) == df._cnt(2) && size (RHS, 2) >=  df._cnt(2)) \
124
    if ((length (indc) == df._cnt(2) && size (RHS, 2) >=  df._cnt(2)) ...
125
        || 0 == df._cnt(2) || isempty (S.subs{1}) || isempty (S.subs{2}))
125
        || 0 == df._cnt(2) || isempty (S.subs{1}) || isempty (S.subs{2}))
126
      %# providing too much information -- remove extra content
126
      %# providing too much information -- remove extra content
127
      if (size (RHS, 1) > 1)
127
      if (size (RHS, 1) > 1)
128
        %# at this stage, verify that the first line doesn't contain
128
        %# at this stage, verify that the first line doesn't contain
129
        %# chars only; use them for column names
129
        %# chars only; use them for column names
130
        dummy = cellfun ('class', \
130
        dummy = cellfun ('class', ...
131
                         RHS(1, ~cellfun ('isempty', RHS(1, :))), \
131
                         RHS(1, ~cellfun ('isempty', RHS(1, :))), ...
132
                         'UniformOutput', false);
132
                         'UniformOutput', false);
133
        dummy = strcmp (dummy, 'char');
133
        dummy = strcmp (dummy, 'char');
134
        if (all (dummy))
134
        if (all (dummy))
135
          if (length (df._over{2}) >= max (indc) \
135
          if (length (df._over{2}) >= max (indc) ...
136
              && ~all (df._over{2}(indc)) && ~isempty (S.subs{2}))
136
              && ~all (df._over{2}(indc)) && ~isempty (S.subs{2}))
137
            warning("Trying to overwrite colum names");
137
            warning("Trying to overwrite colum names");
138
          endif
138
          endif
139
          
139
          
140
          cname = RHS(1, :).'; RHS = RHS(2:end, :);            
140
          cname = RHS(1, :).'; RHS = RHS(2:end, :);            
...
...
154
            endif
154
            endif
155
          endif
155
          endif
156
        endif
156
        endif
157
        %# at this stage, verify that the first line doesn't contain
157
        %# at this stage, verify that the first line doesn't contain
158
        %# chars only; use them for column types
158
        %# chars only; use them for column types
159
        dummy = cellfun ('class', \
159
        dummy = cellfun ('class', ...
160
                         RHS(1, ~cellfun ('isempty', RHS(1, :))), \
160
                         RHS(1, ~cellfun ('isempty', RHS(1, :))), ...
161
                         'UniformOutput', false);
161
                         'UniformOutput', false);
162
        dummy = strcmp (dummy, 'char');
162
        dummy = strcmp (dummy, 'char');
163
        if (all (dummy))
163
        if (all (dummy))
164
          if (length (df._over{2}) >= max (indc) \
164
          if (length (df._over{2}) >= max (indc) ...
165
              && ~all (df._over{2}(indc)))
165
              && ~all (df._over{2}(indc)))
166
            warning ("Trying to overwrite colum names");
166
            warning ("Trying to overwrite colum names");
167
          endif
167
          endif
168
          
168
          
169
          if (sum (~cellfun ('isempty', RHS(1, indc))) == ncol)
169
          if (sum (~cellfun ('isempty', RHS(1, indc))) == ncol)
...
...
178
      endif
178
      endif
179
      
179
      
180
      %# more elements than df width -- try to use the first two as
180
      %# more elements than df width -- try to use the first two as
181
      %# row index and/or row name
181
      %# row index and/or row name
182
      if (size (RHS, 1) > 1)
182
      if (size (RHS, 1) > 1)
183
        dummy = all (cellfun ('isnumeric', \
183
        dummy = all (cellfun ('isnumeric', ...
184
                              RHS(~cellfun ('isempty', RHS(:, 1)), 1)));
184
                              RHS(~cellfun ('isempty', RHS(:, 1)), 1)));
185
      else
185
      else
186
        dummy =  isnumeric(RHS{1, 1});
186
        dummy =  isnumeric(RHS{1, 1});
187
      endif
187
      endif
188
      dummy = dummy && (~isempty (cname) && size (cname{1}, 2) < 1);
188
      dummy = dummy && (~isempty (cname) && size (cname{1}, 2) < 1);
...
...
209
      endif
209
      endif
210
      
210
      
211
      if (size (RHS, 2) >  df._cnt(2))
211
      if (size (RHS, 2) >  df._cnt(2))
212
        %# verify the the first row doesn't contain chars only, use them
212
        %# verify the the first row doesn't contain chars only, use them
213
        %# for row names
213
        %# for row names
214
        dummy = cellfun ('class', \
214
        dummy = cellfun ('class', ...
215
                         RHS(~cellfun ('isempty', RHS(:, 1)), 1), \
215
                         RHS(~cellfun ('isempty', RHS(:, 1)), 1), ...
216
                         'UniformOutput', false);
216
                         'UniformOutput', false);
217
        dummy = strcmp (dummy, 'char') \
217
        dummy = strcmp (dummy, 'char') ...
218
            && (~isempty (cname) && size (cname{1}, 2) < 1);
218
            && (~isempty (cname) && size (cname{1}, 2) < 1);
219
        if (all (dummy)) 
219
        if (all (dummy)) 
220
          if (length (df._over{1}) >= max (indr) \
220
          if (length (df._over{1}) >= max (indr) ...
221
              && ~all (df._over{1}(indr)))
221
              && ~all (df._over{1}(indr)))
222
            warning("Trying to overwrite row names");
222
            warning("Trying to overwrite row names");
223
          else
223
          else
224
            rname = RHS(:, 1); 
224
            rname = RHS(:, 1); 
225
          endif
225
          endif
...
...
322
        %# whole assignement
322
        %# whole assignement
323
        try 
323
        try 
324
          if (size (RHS, 1) <= 1)
324
          if (size (RHS, 1) <= 1)
325
            switch df._type{indc(indi)}
325
            switch df._type{indc(indi)}
326
              case {'char' } %# use a cell array to hold strings
326
              case {'char' } %# use a cell array to hold strings
327
                dummy = cellfun (@num2str, RHS(:, indj), \
327
                dummy = cellfun (@num2str, RHS(:, indj), ...
328
                                 'UniformOutput', false);
328
                                 'UniformOutput', false);
329
              case {'double' }
329
              case {'double' }
330
                dummy = fillfunc (indj);
330
                dummy = fillfunc (indj);
331
              otherwise
331
              otherwise
332
                dummy = cast(fillfunc (indj), df._type{indc(indi)});
332
                dummy = cast(fillfunc (indj), df._type{indc(indi)});
...
...
336
            idxOK = ~cellfun ('isempty', RHS(:, indj));
336
            idxOK = ~cellfun ('isempty', RHS(:, indj));
337
            %# intialise dummy so that it can receive "anything"
337
            %# intialise dummy so that it can receive "anything"
338
            dummy = [];
338
            dummy = [];
339
            switch (df._type{indc(indi)})
339
            switch (df._type{indc(indi)})
340
              case {'char' } %# use a cell array to hold strings
340
              case {'char' } %# use a cell array to hold strings
341
                dummy = cellfun (@num2str, RHS(:, indj), \
341
                dummy = cellfun (@num2str, RHS(:, indj), ...
342
                                 'UniformOutput', false);
342
                                 'UniformOutput', false);
343
              case {'double' }
343
              case {'double' }
344
                dummy(idxOK, :) = fillfunc (indj); dummy(~idxOK, :) = NA;
344
                dummy(idxOK, :) = fillfunc (indj); dummy(~idxOK, :) = NA;
345
              otherwise
345
              otherwise
346
                dummy(idxOK, :) = fillfunc (indj); dummy(~idxOK, :) = NA;
346
                dummy(idxOK, :) = fillfunc (indj); dummy(~idxOK, :) = NA;
...
...
371
                  dummy(indk) = mat2str (dummy{indk}, 6);
371
                  dummy(indk) = mat2str (dummy{indk}, 6);
372
                endif
372
                endif
373
              endif
373
              endif
374
            endfor
374
            endfor
375
          else
375
          else
376
            dummy = \
376
            dummy = ...
377
                sprintf ("Assignement failed for colum %d, of type %s and length %d,\nwith new content\n%s", \
377
                sprintf ("Assignement failed for colum %d, of type %s and length %d,\nwith new content\n%s", ...
378
                         indj, df._type{indc(indi)}, length (indr), disp (RHS(:, indj)));
378
                         indj, df._type{indc(indi)}, length (indr), disp (RHS(:, indj)));
379
            error (dummy);
379
            error (dummy);
380
          endif
380
          endif
381
        end_try_catch
381
        end_try_catch
382
        if (size (dummy, 1) < df._cnt(1))
382
        if (size (dummy, 1) < df._cnt(1))
...
...
386
        %# partial assignement -- extract actual data and update
386
        %# partial assignement -- extract actual data and update
387
        dummy = df._data{indc(indi)}; 
387
        dummy = df._data{indc(indi)}; 
388
        try     
388
        try     
389
          switch (df._type{indc(indi)})
389
          switch (df._type{indc(indi)})
390
            case {'char' } %# use a cell array to hold strings
390
            case {'char' } %# use a cell array to hold strings
391
              dummy(indr, 1) = cellfun(@num2str, RHS(:, indj), \
391
              dummy(indr, 1) = cellfun(@num2str, RHS(:, indj), ...
392
                                       'UniformOutput', false);
392
                                       'UniformOutput', false);
393
            case {'double' }
393
            case {'double' }
394
              dummy(indr, :) = fillfunc (indj);
394
              dummy(indr, :) = fillfunc (indj);
395
            otherwise
395
            otherwise
396
              dummy(indr, :) = cast(fillfunc (indj), df._type{indc(indi)});
396
              dummy(indr, :) = cast(fillfunc (indj), df._type{indc(indi)});
397
          endswitch
397
          endswitch
398
        catch
398
        catch
399
          dummy = \
399
          dummy = ...
400
              sprintf ("Assignement failed for colum %d, of type %s and length %d,\nwith new content\n%s", \
400
              sprintf ("Assignement failed for colum %d, of type %s and length %d,\nwith new content\n%s", ...
401
                       indj, df._type{indc(indi)}, length (indr), disp (RHS(:, indj)));
401
                       indj, df._type{indc(indi)}, length (indr), disp (RHS(:, indj)));
402
          error (dummy);
402
          error (dummy);
403
        end_try_catch
403
        end_try_catch
404
      endif
404
      endif
405
      df._data{indc(indi)} = dummy; df._rep{indc(indi)} = 1:size (dummy, 2); 
405
      df._data{indc(indi)} = dummy; df._rep{indc(indi)} = 1:size (dummy, 2); 
...
...
407
    endfor
407
    endfor
408
408
409
  else 
409
  else 
410
    %# RHS is either a numeric, either a df
410
    %# RHS is either a numeric, either a df
411
    if (any (indc > min (size (df._data, 2), df._cnt(2))))
411
    if (any (indc > min (size (df._data, 2), df._cnt(2))))
412
      df = df_pad(df, 2, max (indc-min (size (df._data, 2), df._cnt(2))),\
412
      df = df_pad(df, 2, max (indc-min (size (df._data, 2), df._cnt(2))),...
413
                   class(RHS));
413
                   class(RHS));
414
    endif
414
    endif
415
    if (~isempty (inds) && isnumeric(inds) && any (inds > 1))
415
    if (~isempty (inds) && isnumeric(inds) && any (inds > 1))
416
      for indi = (1:ncol)
416
      for indi = (1:ncol)
417
        if (max (inds) > length (df._rep{indc(indi)}))
417
        if (max (inds) > length (df._rep{indc(indi)}))
418
          df = df_pad(df, 3, max (inds)-length (df._rep{indc(indi)}), \
418
          df = df_pad(df, 3, max (inds)-length (df._rep{indc(indi)}), ...
419
                      indc(indi));
419
                      indc(indi));
420
        endif
420
        endif
421
      endfor
421
      endfor
422
    endif
422
    endif
423
423
...
...
431
      S.subs(2) = []; Sorig = S; 
431
      S.subs(2) = []; Sorig = S; 
432
      for indi = (1:ncol)
432
      for indi = (1:ncol)
433
        [df, S] = df_cow(df, S, indc(indi));
433
        [df, S] = df_cow(df, S, indc(indi));
434
        if (strcmp (df._type(indc(indi)), RHS._type(indi)))
434
        if (strcmp (df._type(indc(indi)), RHS._type(indi)))
435
          try
435
          try
436
            df._data{indc(indi)} = feval (@subsasgn, df._data{indc(indi)}, S, \
436
            df._data{indc(indi)} = feval (@subsasgn, df._data{indc(indi)}, S, ...
437
                                          RHS._data{indi}(:, RHS._rep{indi}));
437
                                          RHS._data{indi}(:, RHS._rep{indi}));
438
          catch
438
          catch
439
            disp (lasterr ()); disp('line 516 ???'); keyboard
439
            disp (lasterr ()); disp('line 516 ???'); keyboard
440
          end_try_catch
440
          end_try_catch
441
        else
441
        else
442
          df._data{indc(indi)} = feval (@subsasgn, df._data{indc(indi)}, S, \
442
          df._data{indc(indi)} = feval (@subsasgn, df._data{indc(indi)}, S, ...
443
                                        cast (RHS._data{indi}(:, RHS._rep{indi}),\
443
                                        cast (RHS._data{indi}(:, RHS._rep{indi}),...
444
                                            df._type(indc(indi))));
444
                                            df._type(indc(indi))));
445
        endif
445
        endif
446
        S = Sorig;
446
        S = Sorig;
447
      endfor
447
      endfor
448
      if (~isempty (RHS._name{1}))
448
      if (~isempty (RHS._name{1}))
...
...
544
      error("row indexes are not unique or not ordered");
544
      error("row indexes are not unique or not ordered");
545
    endif
545
    endif
546
    df._ridx = dummy;
546
    df._ridx = dummy;
547
  endif
547
  endif
548
  
548
  
549
  if (~isempty (rname) && (length (df._over{1}) < max (indr) || \
549
  if (~isempty (rname) && (length (df._over{1}) < max (indr) || ...
550
        all (df._over{1}(indr))))
550
        all (df._over{1}(indr))))
551
    df._name{1}(indr, 1) = genvarname(rname);
551
    df._name{1}(indr, 1) = genvarname(rname);
552
    df._over{1}(1, indr) = false;
552
    df._over{1}(1, indr) = false;
553
  endif
553
  endif
554
  if (~isempty (cname) && (length (df._over{2}) < max (indc) || \
554
  if (~isempty (cname) && (length (df._over{2}) < max (indc) || ...
555
        all (df._over{2}(indc))))
555
        all (df._over{2}(indc))))
556
    if (length (cname) < ncol)
556
    if (length (cname) < ncol)
557
      cname(end+1:ncol) = {'_'};
557
      cname(end+1:ncol) = {'_'};
558
    endif
558
    endif
559
    cname(cellfun (@isempty, cname)) = 'unnamed';
559
    cname(cellfun (@isempty, cname)) = 'unnamed';

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks