Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

dataset

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:

result

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!

share|improve this question
1  
Sorry, you need to set the Listable attribute: SetAttributes[fnStr2Real, {Listable}]; –  akm Jul 17 at 9:51
    
Why don't you use ToExpression for the conversion? You could check before conversion whether the string contains numeric data using StringMatchQ. –  Sjoerd C. de Vries Jul 17 at 10:41
    
InternalStringToDouble is fine. Check InternalStringToDouble /@ {"1234", "1234.", "1234.567", "1.234e+3", "1.23456e+3", "0.5664", "5.664e-1", "5.664e-4"} –  akm Jul 17 at 11:14
    
@SjoerdC.deVries ToExpression is very slow compared to Internal`StringToDouble. See this –  Murta Jul 17 at 13:19

1 Answer 1

up vote 3 down vote accepted

I don't yet know where the rounding is coming from but it appears to be occurring during formatting.

Consider:

testset[fnStr2Real, {"a", "b"}] // Normal // Normal
{{"a" -> 529251., "b" -> 520358.}, {"a" -> 51., "b" -> 0},
 {"a" -> 177., "b" -> 0}, {"a" -> 125., "b" -> 0}, {"a" -> 1343., "b" -> 0}}

Actually the rounding seems to be a standard part of the formatting of Dataset:

Dataset[<|a -> 123456.|>]

enter image description here

share|improve this answer
    
Yes, you're right; interesting. It also appears correctly in the FullForm. So I don't need to focus on this and can continue with my work. Everything is fine; it's just a matter of vizualisation –  akm Jul 17 at 10:11
    
@akm I think that's correct, but it's still surprising. I haven't yet read about Datasets so this may be plainly documented. Perhaps there is an Option that controls it. I'll try to remember to update this post later as I learn more. –  Mr.Wizard Jul 17 at 10:12
    
Maybe, the programmers have remembered this: (stat.washington.edu/pds/stat423/Documents/…), which is actually good, but should be documented. –  akm Jul 17 at 10:17
4  
@Mr.Wizard It's neither documented nor controllable. The formatting of Dataset is quite limited at the moment, there are several ways I wish to expand it and make it more interactive, so making it controllable now is premature and would probably lock the design to something sub-optimal. –  Taliesin Beynon Jul 17 at 13:46
    
@TaliesinBeynon Thanks for the information. –  Mr.Wizard Jul 17 at 13:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.