]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/@dataframe/private/df_pad.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / @dataframe / private / df_pad.m
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:
5   %# dim = 1 => not used
6   %# dim = 2 => type of the added column(s)
7   %# dim = 3 => index of columns receiving a new sheet (default: all)
8
9   %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
10   %%
11   %% This file is part of Octave.
12   %%
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.
17   %%
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
22   %% details.
23   %%
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.
28   
29   %#
30   %# $Id: df_pad.m 9585 2012-02-05 15:32:46Z cdemills $
31   %#
32
33   switch dim
34     case 1
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;
40         endif
41       endif
42       %# complete row indexes: by default, row number.
43       if (isempty(df._ridx)),
44         dummy = (1:n)(:);
45       else
46         dummy = vertcat(df._ridx, repmat(size(df._ridx, 1)+(1:n)(:), ...
47                                          1, size(df._ridx, 2))); 
48       endif
49       df._ridx = dummy; 
50       %# pad every line
51       for indi = 1:min(size(df._data, 2), df._cnt(2)),
52         neff = n + df._cnt(1) - size(df._data{indi}, 1);
53         if (neff > 0),
54           m = size(df._data{indi}, 2);
55           switch df._type{indi}
56             case {'char'}
57               dummy = {}; dummy(1:neff, 1:m) = NA;
58               dummy = vertcat(df._data{indi}, dummy);
59             case { 'double' }
60               dummy = vertcat(df._data{indi}, repmat(NA, neff, m));
61             otherwise
62               dummy = cast(vertcat(df._data{indi}, repmat(NA, neff, m)), ...
63                            df._type{indi});
64           endswitch
65           df._data{indi} = dummy;
66         endif
67       endfor
68       df._cnt(1) = df._cnt(1) + n;
69
70     case 2
71       %# create new columns
72       if (isempty(coltype))
73         error("df_pad: dim equals 2, and coltype undefined");
74       endif
75       if (length(n) > 1), %#second value is an offset
76         indc =  n(2); n = n(1);
77         if (indc < df._cnt(2)),
78           %# shift to the right
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;
91         endif
92       else
93         %# add new values after the last column
94         indc = min(size(df._data, 2), df._cnt(2)); 
95       endif
96       if (!isa(coltype, 'cell')), coltype = {coltype}; endif
97       if (isscalar(coltype) && n > 1),
98         coltype = repmat(coltype, 1, n);
99       endif
100       for indi = (1:n),
101         switch coltype{indi}
102           case {'char'}
103             dummy = {repmat(NA, df._cnt(1), 1) }; 
104             dummy(:, 1) = '_';
105           case { 'double'}
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);
109           otherwise
110             dummy = cast(repmat(NA, df._cnt(1), 1), coltype{indi});
111         endswitch
112         df._data{indc+indi} = dummy;
113         df._rep{indc+indi} = 1;
114         df._type{indc+indi} = coltype{indi};
115       endfor
116    
117       if (size(df._data, 2) > df._cnt(2)),
118         df._cnt(2) =  size(df._data, 2);
119       endif
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;
126       endif   
127       
128     case 3
129       if (n <= 0), return; endif
130       if (isempty(coltype)),
131         coltype = 1:df._cnt(2);
132       endif
133       dummy = max(n+cellfun(@length, df._rep(coltype)));
134       if (size(df._ridx, 2) < dummy),
135         df._ridx(:, end+1:dummy) = NA;
136       endif
137       for indi = coltype,
138         switch df._type{indi}
139           case {'char'}
140             if (isa(df._data{indi}, 'char')),
141               dummy = horzcat(df._data{indi}(:, df._rep{indi}), \
142                               {repmat(NA, df._cnt(1), 1)});
143             else
144               dummy = df._data{indi};
145             endif
146           case { 'double' }
147             dummy = horzcat(df._data{indi}(:, df._rep{indi}), \
148                             repmat(NA, df._cnt(1), 1));
149           case { 'logical' }
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));
153           otherwise
154             dummy = cast(horzcat(df._data{indi}(:, df._rep{indi}), \
155                                  repmat(NA, df._cnt(1), 1)), \
156                          df._type{indi});
157         endswitch
158         df._data{indi} = dummy;
159         df._rep{indi} = [df._rep{indi} length(df._rep{indi})+ones(1, n)];
160       endfor
161         df =  df_thirddim(df);
162     otherwise
163       error('Invalid dimension in df_pad');
164   endswitch
165
166 endfunction