i'm trying to get familiar with the new Dataset-stuff in Mathematica 10. As it will (hopefully) provide some benefit for me, I have started to convert my app to move from list and list of rules to Associations and Datasets.
Starting point for me is an XML object, which is converted to a Dataset now. I was hoping to reduce the programming code by e.g. applying functions to multiple columns at once.
This is an example Dataset:
testset = Dataset[{<|"Element" -> "A", "a" -> "529251", "b" -> "520358"|>, <|
"Element" -> "B", "a" -> "51"|>, <|"Element" -> "C",
"a" -> "177"|>, <|"Element" -> "L", "a" -> "125"|>, <|
"Element" -> "S", "a" -> "1343"|>}]
This gives:
As the XML sometimes contains integers and reals in scientific form, I'm using
Internal`StringToDouble
to convert the stuff:
fnStr2Real[x_,r_:0]:=If[Head[x]===Missing,r,Internal`StringToDouble[x]]
SetAttributes[fnStr2Real, {Listable}];
testset[fnStr2Real, {"a", "b"}]
(* Trace yields *)
(* Query[fnStr2Real, {"a", "b"}][testset] *)
The issue: there is some kind of rounding in the resulting recordset:
If I apply my function directly to my list or association, everything is fine:
Function[x, fnStr2Real[#[x] & /@ Normal[testset]]] /@ {"a", "b"}
(* {{529251., 51., 177., 125., 1343.}, {520358., 0, 0, 0, 0}} *)
I know that Internal`StringToDouble is an internal function, but I found several posts in this forum using it, and I really like it!
Is there an issue with M10, Dataset and Internal`StringToDouble?
Are there any other robust string coversion functions availabe which can handle integers, reals and reals in scientific form inside a dataset? Thanks!
ToExpression
for the conversion? You could check before conversion whether the string contains numeric data usingStringMatchQ
. $\endgroup$StringToDouble is fine. Check Internal
StringToDouble /@ {"1234", "1234.", "1234.567", "1.234e+3", "1.23456e+3", "0.5664", "5.664e-1", "5.664e-4"} $\endgroup$ToExpression
is very slow compared toInternal`StringToDouble
. See this $\endgroup$