REPLACECOL

Purpose:

Replaces a column of a table.

Syntax:

REPLACECOL(series, newcol, colnum, inplace)

series

-

A multi-column series.

newcol

-

A series, the new column to replace the existing column.

colnum

-

Optional, an integer,  the column number to replace.

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

If colnum > numcols, a new column is appended after the last column. Defaults to numcols+1, 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: replacecol(W1, 1..3)

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

W4: replacecol(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   1   9

2   2  10

3   3

 

W4 contains the table:

 

1   4   1

2   5   2

3   6   3

    7

    8

Example:

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

W2: replacecol(W1, ones(2, 2), 2)

 

W1 contains the table:

 

1   4   9

2   5  10

3   6

    7

    8

 

W2 contains the table:

 

1   1   1   9

2   1   1  10

3              

Example:

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

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

a = replacecol(a, {100, 101, 102}, 2);

replacecol(b, {100, 101, 102}, 2, 1);

 

Initially:

 

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

      {2, 5, 8},

      {3, 6, 9}}

 

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

      {2, 5, 8},

      {3, 6, 9}}

 

After replacement:

 

a == {{1, 100, 5},

      {2, 101, 5},

      {3, 102, 5}}

 

b == {{1, 100, 5},

      {2, 101, 5},

      {3, 102, 5}}

 

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

Remarks:

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

 

As shown in the second example, if the new series contains multiple columns, all of the new columns replace the original column.

 

Columns can also be replaced by assigning a series to a column. For example:

 

a = ravel(1..16, 4);

a[.., 3] = 100..103;

 

a == {{1, 5, 100, 13},

      {2, 6, 101, 14}, 

      {3, 7, 102, 15}, 

      {4, 8, 103, 16}} 

 

See INSERTCOL to insert one or more columns into a table.

See Also:

COLEXTRACT

DELETE

DELETECOL

DELETEROW

INSERTCOL

RAVEL

REMOVE

REPLACE

RESHAPE