INSERTCOL

Purpose:

Inserts a column in a table.

Syntax:

INSERTCOL(series, newcol, colnum, inplace)

series

-

A series or table.

newcol

-

A series, the new column to insert.

colnum

-

Optional. An integer, the column number insertion location:

If colnum < 0, the new column is inserted before the first column.

If colnum >= numcols, the new column is appended after the last column. Defaults to numcols, append new column to table.

inplace

-

Optional. An integer, the operate in-place flag:

0: do not operate in-place (default).

1: operate in-place.

Returns:

A series or table.

Example:

W1: reshape(1..10, {3, 5, 2})

W2: insertcol(W1, 1..3)

W3: insertcol(W1, 1..3, 2)

W4: insertcol(W1, 1..3, 3)

 

W1 contains the table:

 

1   4   9

2   5  10

3   6

    7

    8

 

W2 contains the table:

 

1   4   9   1

2   5  10   2

3   6       3

    7

    8

 

W3 contains the table:

 

1   4   1   9

2   5   2  10

3   6   3

    7

    8

 

W4 contains the table:

 

1   4   9   1

2   5   10  2

3   6       3

    7

    8

Example:

W1: reshape(1..10, {3, 5, 2})

W2: insertcol(W1, ones(2, 2), 1)

 

W1 contains the table:

 

1   4   9

2   5  10

3   6

    7

    8

 

W2 contains the table:

 

1   1   1   4   9

2   1   1   5  10

3           6    

            7

            8

 

Multiple columns are inserted after column 1.

Example:

a = ravel(1..9, 3);

b = ravel(1..9, 3);

a = insertcol(a, {5, 5, 5}, 2);

insertcol(b, {5, 5, 5}, 2, 1);

 

Initially:

 

a == {{1, 4, 7},

      {2, 5, 8},

      {3, 6, 9}}

 

b == {{1, 4, 7},

      {2, 5, 8},

      {3, 6, 9}}

 

After insertion:

 

a == {{1, 4, 5, 7},

      {2, 5, 5, 8},

      {3, 6, 5, 9}}

 

b == {{1, 4, 5, 7},

      {2, 5, 5, 8},

      {3, 6, 5, 9}}

 

A new column is inserted at after column 2 for both arrays. However, variable a is reassigned to an entirely new array object, while the column is inserted directly into the original array of variable b (in-place modification).

Remarks:

By default, INSERTCOL does not modify the original data and instead returns a new series. For very large arrays where memory allocation is costly, enabling in-place insertion with inplace=1 can significantly improve performance.

 

As shown in the second example, if the new series contains multiple columns, all columns are inserted.

 

See REPLACECOL to replace an existing column with new values.

See Also:

COLEXTRACT

DELETE

DELETECOL

DELETEROW

INSERT

REPLACE

REPLACECOL

RESHAPE

REMOVE