Function Repository Resource:

ColumnMap

Source Notebook

Map functions over columns instead of rows

Contributed by: Michael Sollami

ResourceFunction["ColumnMap"][f,list,n]

maps the function f over the nth column of a table list.

ResourceFunction["ColumnMap"][f,list,{n1,n2,}]

maps the function f over the columns n1,n2, … of a table list.

ResourceFunction["ColumnMap"][{f1,f2,},list,{n1,n2,}]

maps the function fi over the column ni of a table list.

Details and Options

ResourceFunction["ColumnMap"] requires its second argument list to be of at least Depth three.
ResourceFunction["ColumnMap"][f,list,n] is equivalent to MapAt[f,list,{All,n}].

Examples

Basic Examples (4) 

Specify functions to map over each column of a table:

In[1]:=
ResourceFunction["ColumnMap"][{f, g, h}, {{a, b, c}}, {1, 2, 3}]
Out[1]=

Map a function over a column:

In[2]:=
ResourceFunction["ColumnMap"][Sqrt, ConstantArray[2, {3, 5}], 4] // TextGrid
Out[2]=

Map a function over specific columns:

In[3]:=
ResourceFunction["ColumnMap"][ #^2 &, ConstantArray[x, {4, 6}], {1, 3, 5}] // MatrixForm
Out[3]=

Map multiple functions over multiple columns:

In[4]:=
zeros = Table[0, {i, 5}, {j, 5}];
ResourceFunction["ColumnMap"][ {# + 1 &, # - 1 &}, zeros, {2, 4}] // TableForm
Out[5]=

Scope (1) 

ColumnMap's column indices may be either positive or negative:

In[6]:=
ResourceFunction["ColumnMap"][
  Framed, {{1, 2, 3}}, {1, -1}] // TableForm
Out[6]=

Possible Issues (1) 

ColumnMap works on lists that have columns (i.e. lists with Depth 3):

In[7]:=
(l |-> Labeled[ResourceFunction["ColumnMap"][Style[#, Bold] &, l, 2], Depth@l]) /@ {Range[5], {Range[5]}, {{Range[5]}\[Transpose]}}
Out[7]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 18 February 2020

Related Resources

License Information