I want to modify a list of table row by adding an extra column. For that I Map
the data with a pure function that evluate the new column value from exisiting one and reconstruct a new list from that and the initial Part
s of the list:
b = 5000
data = {{1, 45000., 27500., "Inverted"},
{2, 22500., 18333.3, ""},
{3, 15000., 13750., "Inverted"},
{4, 11250., 11000., ""},
{5, 9000., 9166.67, "Inverted"},
{6, 7500., 7857.14, ""},
{7, 6428.57, 6875., "Inverted"}}
{#[[1;;3]], #[[1]]> 2b, #[[4]]} & /@ data
{{{1,45000.,27500.},False,Inverted}, {{2,22500.,18333.3},False,}, {{3,15000.,13750.},False,Inverted}, {{4,11250.,11000.},False,}, {{5,9000.,9166.67},False,Inverted}, {{6,7500.,7857.14},False,}, {{7,6428.57,6875.},False,Inverted}}
The problem is #[[1;;3]]
return a list--so I end-up with nested list instead of flat records.
As a workarround, I Flatten
each record:
Flatten[{#[[1;;3]], #[[1]]> 2b, #[[4]]}] & /@ data
{{1,45000.,27500.,False,Inverted}, {2,22500.,18333.3,False,}, {3,15000.,13750.,False,Inverted}, {4,11250.,11000.,False,}, {5,9000.,9166.67,False,Inverted}, {6,7500.,7857.14,False,}, {7,6428.57,6875.,False,Inverted}}
Flatten[{#[[1;;3]], #[[1]]> 2b, #[[4]]}] & /@ data
It works in that particular case. But it is not entirely satisfactory since, if the initial record would already contain list items, they would have been flattened too.
If there a more generic solution to build a list from items and list spans?
:>
notation. I need to take a look at that! $\endgroup$@@@
(i.e. Apply at level 1) andRuleDelayed
are worth looking into! $\endgroup$