Pregunta

Examples: Say I have several 1D arrays, such as A, B, and C:

 A    B    C
---  ---  ---
 2    4    99
 3    5    37
 4    6    42
      7

I want to use a formula to pad each of these arrays (which are named ranges) so they have the same number of rows. (Edited, to provide a more complete explanation:) I would also like to be able to shift the starting position of each array by a specified number of rows. Here is an example output, where I'm shifting A by two, B by three, and C by one position:

 A'   B'   C'
---  ---  ---
 0    0    0
 0    0    99
 2    0    37
 3    0    42
 4    4    0
 0    5    0
 0    6    0
 0    7    0

So the "formula" (really a combination of multiple existing standard formulas) to pad one of the arrays would effectively have four inputs: the original array name/range, the number of values to pad at the beginning of the original array, the number to pad at the end, and the padding value (i.e. 0, NA(), etc.).

Initial approach: My first attempt was to concatenate the three array sections (beginning padding array, original array, and ending padding array) into one array like this:

=INDIRECT("{" & IF(_NPadBegin>0, REPT(_PadVal&",", _NPadBegin-1) & _PadVal&",", "") & _ArrayName & IF(_NPadEnd>0, "," & REPT(_PadVal&",", _NPadEnd-1) & _PadVal, "") &"}")

(where the variables with underscores are named ranges for the four inputs)

However, it doesn't seem possible to build an array like this. I've searched for other methods to combine 1D arrays using a formula, but came up empty.

Another approach might be to create an array of the final desired length and initially populate it with the padding value. For example, create an A' array with eight rows of 0. (I can do this step in a dynamic formula.) Then you could overwrite the values in this initialized array with the original array, making sure to shift the original array to the correct location. For example, overwrite the 0 values in rows 2 through 4 of A' with the values of A. (I can't figure out how to do this second step...)

Background: I have multiple 1D arrays of data (generated using dynamic named ranges) that I would like to plot together on a single line chart. The arrays have differing lengths however, so I need to pad each array with a certain number of NA() values at the beginning/end of the array. They also need to be "lined up" based on an index.

It probably wouldn't be too difficult to achieve this in VBA, but I'd prefer a (non-user-defined) formula-based solution.

¿Fue útil?

Solución

If you have values 2, 3, and 4 in A2:A4 try this formula

=IF(COUNTIF(A2:A4,ROW(INDIRECT("1:"&D2))-1),ROW(INDIRECT("1:"&D2))-1,0)

where D2 contains the number of values (8 in your example).

That will return this array

{0;0;2;3;4;0;0;0}

assumes that number series starts at zero - the zero at the end of the formula is the "padding value", change as required

OK as per comments you can do that with this array formula

=IF((ROW(INDIRECT("1:"&NPadBegin+NPadEnd+ROWS(_ArrayName)))>NPadBegin)*(ROW(INDIRECT("1:"&NPadBegin+NPadEnd+ROWS(_ArrayName)))<=ROWS(_ArrayName)+NPadBegin),N(OFFSET(_ArrayName,ROW(INDIRECT("1:"&NPadBegin+NPadEnd+ROWS(_ArrayName)))-1-NPadBegin,0,1)),PadVal)

assumes that _ArrayName is a vertical 1D array

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top