1 function df = df_pad(df, dim, n, coltype=[])
2 %# function resu = df_pad(df, dim, n, coltype = [])
3 %# given a dataframe, insert n rows or columns, and adjust everything
4 %# accordingly. Coltype is a supplemental argument:
6 %# dim = 2 => type of the added column(s)
7 %# dim = 3 => index of columns receiving a new sheet (default: all)
9 %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
11 %% This file is part of Octave.
13 %% Octave is free software; you can redistribute it and/or
14 %% modify it under the terms of the GNU General Public
15 %% License as published by the Free Software Foundation;
16 %% either version 2, or (at your option) any later version.
18 %% Octave is distributed in the hope that it will be useful,
19 %% but WITHOUT ANY WARRANTY; without even the implied
20 %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21 %% PURPOSE. See the GNU General Public License for more
24 %% You should have received a copy of the GNU General Public
25 %% License along with Octave; see the file COPYING. If not,
26 %% write to the Free Software Foundation, 51 Franklin Street -
27 %% Fifth Floor, Boston, MA 02110-1301, USA.
30 %# $Id: df_pad.m 9585 2012-02-05 15:32:46Z cdemills $
35 if (!isempty(df._name{1})),
36 if (length(df._name{1}) < df._cnt(1)+n),
37 %# generate a name for the new row(s)
38 df._name{1}(df._cnt(1)+(1:n), 1) = {'_'};
39 df._over{1}(1, df._cnt(1)+(1:n), 1) = true;
42 %# complete row indexes: by default, row number.
43 if (isempty(df._ridx)),
46 dummy = vertcat(df._ridx, repmat(size(df._ridx, 1)+(1:n)(:), ...
47 1, size(df._ridx, 2)));
51 for indi = 1:min(size(df._data, 2), df._cnt(2)),
52 neff = n + df._cnt(1) - size(df._data{indi}, 1);
54 m = size(df._data{indi}, 2);
57 dummy = {}; dummy(1:neff, 1:m) = NA;
58 dummy = vertcat(df._data{indi}, dummy);
60 dummy = vertcat(df._data{indi}, repmat(NA, neff, m));
62 dummy = cast(vertcat(df._data{indi}, repmat(NA, neff, m)), ...
65 df._data{indi} = dummy;
68 df._cnt(1) = df._cnt(1) + n;
73 error("df_pad: dim equals 2, and coltype undefined");
75 if (length(n) > 1), %#second value is an offset
76 indc = n(2); n = n(1);
77 if (indc < df._cnt(2)),
79 df._name{2}(n + (indc+1:end)) = df._name{2}(indc+1:end);
80 df._over{2}(n + (indc+1:end)) = df._over{2}(indc+1:end);
81 dummy = cstrcat(repmat('_', n, 1), ...
82 strjust(num2str(indc + (1:n).'), 'left'));
83 df._name{2}(indc + (1:n)) = cellstr(dummy);
84 df._over{2}(indc + (1:n)) = true;
85 df._type(n+(indc+1:end)) = df._type(indc+1:end);
86 df._type(indc + (1:n)) = NA;
87 df._data(n + (indc+1:end)) = df._data(indc+1:end);
88 df._rep(n + (indc+1:end)) = df._rep(indc+1:end);
89 df._data(indc + (1:n)) = NA;
90 df._rep(indc + (1:n)) = 1;
93 %# add new values after the last column
94 indc = min(size(df._data, 2), df._cnt(2));
96 if (!isa(coltype, 'cell')), coltype = {coltype}; endif
97 if (isscalar(coltype) && n > 1),
98 coltype = repmat(coltype, 1, n);
103 dummy = {repmat(NA, df._cnt(1), 1) };
106 dummy = repmat(NA, df._cnt(1), 1);
107 case {'logical'} %# there is no NA in logical type
108 dummy = repmat(false, df._cnt(1), 1);
110 dummy = cast(repmat(NA, df._cnt(1), 1), coltype{indi});
112 df._data{indc+indi} = dummy;
113 df._rep{indc+indi} = 1;
114 df._type{indc+indi} = coltype{indi};
117 if (size(df._data, 2) > df._cnt(2)),
118 df._cnt(2) = size(df._data, 2);
120 if (length(df._name{2}) < df._cnt(2)),
121 %# generate a name for the new column(s)
122 dummy = cstrcat(repmat('_', n, 1), ...
123 strjust(num2str(indc + (1:n).'), 'left'));
124 df._name{2}(indc + (1:n)) = cellstr(dummy);
125 df._over{2}(1, indc + (1:n)) = true;
129 if (n <= 0), return; endif
130 if (isempty(coltype)),
131 coltype = 1:df._cnt(2);
133 dummy = max(n+cellfun(@length, df._rep(coltype)));
134 if (size(df._ridx, 2) < dummy),
135 df._ridx(:, end+1:dummy) = NA;
138 switch df._type{indi}
140 if (isa(df._data{indi}, 'char')),
141 dummy = horzcat(df._data{indi}(:, df._rep{indi}), \
142 {repmat(NA, df._cnt(1), 1)});
144 dummy = df._data{indi};
147 dummy = horzcat(df._data{indi}(:, df._rep{indi}), \
148 repmat(NA, df._cnt(1), 1));
150 %# there is no logical 'NA' -- fill empty elems with false
151 dummy = horzcat(df._data{indi}(:, df._rep{indi}), \
152 repmat(false, df._cnt(1), 1));
154 dummy = cast(horzcat(df._data{indi}(:, df._rep{indi}), \
155 repmat(NA, df._cnt(1), 1)), \
158 df._data{indi} = dummy;
159 df._rep{indi} = [df._rep{indi} length(df._rep{indi})+ones(1, n)];
161 df = df_thirddim(df);
163 error('Invalid dimension in df_pad');