i have a large compositional dataset which contains non-zero and zero values. here is a sample:
data = {{22054., 70.62, 0.37, 14.21, 2.89, 0.6, 2.05, 4.18, 4.04},
{22055., 67.84, 0.52, 14.32, 3.77, 0.91, 3., 2.72, 4.62},
{22581., 62.79, 0.62, 13.79, 7.27, 0.46, 2.92, 1.21, 7.97},
{27601., 0., 0., 0., 0., 0., 0., 0., 0.},
{27602., 0., 0., 0., 0., 0., 0., 0., 0.},
{27603., 0., 0., 0., 0., 0., 0., 0., 0.},
{28681., 0., 0., 0., 0., 0., 0., 0., 0.},
{22757., 61.06, 0.77, 16.62, 5.72, 1.66, 4.66, 2.76, 3.46},
{22803., 64.55, 3.01, 16.3, 0.48, 0.09, 0.05, 0.25, 14.29}};
i'm trying to convert the values from weight percent to mol percent. i wish to use the following function to generate a new array of the same length (at level 1) as the original:
mw = {60.084, 79.866, 101.961, 71.844, 40.304, 56.077, 61.98, 94.2};
molPct[oxw_, mw_] :=
Module[{oxcomp, divmw},
oxcomp = Drop[oxw, None, 1];
divmw = Transpose[Transpose[oxcomp]/mw];
(divmw/ Total[divmw, {2}])*100
];
dataMolPct = molPct[data, mw]
unfortunately, this function fails due to the zero elements within the array (where it tries to divide by zero..). this code seems to work fine when i delete the rows containing zero-values.
I tried the following in an attempt to ignore the 'zero' rows:
molPct[oxw_ /; oxw > 0, mw_] :=
Module[{oxcomp, divmw, oxmol},
oxcomp = Drop[oxw, None, 1];
divmw = Transpose[Transpose[oxcomp]/mw];
(divmw/ Total[divmw, {2}])*100
];
...no luck
as i said, it is important that i end up with an array of the same size (as i will join the new data onto the original array).
it would be nice to know how to do something similar for excluding negative numbers.
any suggestions?
With[{tot = Total@#}, If[tot == 0, ConstantArray[0, Length@#], 100 #/tot]] & /@ (#/mw & /@ Rest /@ data)
produce the output you desire? $\endgroup$molPct[data_,mw_]:=With[{tot = Total@#}, If[tot == 0, ConstantArray[0, Length@#], 100 #/tot]] & /@ (#/mw & /@ Rest /@ data)
- I am just checking if I understood the problem correctly. If it works & you like it, I will post a longer answer $\endgroup$