# Max of imported numerical data will not work

I loaded a list with 400,000 values from a file. The list was loaded correctly as I can get it's length (by Length[]) and I can also plot it.

Why can't I get the Max or Min of this list though? When I try to evaluate the cell, I simply get "Max[all the values of my list]" as output.

My file contains a list of floats, each on its own line, separated by a newline.

list = ReadList["myfile.txt"]


Mathematica converts some of the floats into a weird "integer + float e" format, for example "9 + 1.4443 e". Thus, not all values are numerical but I still need to take them into account.

• Could you provide a small sample of your list (eg. myList[[ 1 ;; 10 ]] or by using Short) ? – gwr Jan 31 '16 at 15:39
• @gwr {0.26304, -0.0398959, -0.330003, -0.565977, -0.734065, -0.813924, \ -0.796839, -0.702942, -0.544771, -0.345005} – Gandalf Schmidt Jan 31 '16 at 16:16
• I copied and pasted your list but I notice that there is a '\' before the 7th element. Could this be what is wrong? – Conor Cosnett Jan 31 '16 at 16:22
• Possible duplicate of Removing not-a-number entries from an array of numbers – garej Jan 31 '16 at 16:33
• @JohnConorCosnett No, the backslash must have been a copy+paste bug. – Gandalf Schmidt Jan 31 '16 at 17:00

## Proper Input of file and checking the list

Make sure that the variable list does contain your file inputs and make sure that Mathematica correctly understands your numbers. For this you might try:

list = SemanticImport[ "myfile.txt", "Number", "List" ]


or using ReadList you might try:

list = ReadList[ "myfile.txt", "Number" ]


Then check whether there still is anything besides numeric quantities:

list // Map[ NumericQ] // Apply[And]


If everything is ok, then you should see True as a result.

To further ponder the Plus problem you should check for what exactly happens by looking at the result of:

Select[ list, Head[#] == Plus & ][[1]] // FullForm


As we have discussed in the comments here, you probably have read in some number like "10e+3" which causes problems if not addressed by giving the type as in above uses of SemanticImport or ReadList.

## Finding the Max and Min anyway

If your result was False you may get some result anyway but simply just selecting the numeric elements:

list // RightComposition[
Select[NumericQ],
Through[{Max, Min}[#]] &
]


## Fixing the Plus expressions returned for floats

To address the problem of Plus given for exponential numbers you may also try:

list = list /. Plus[ exp_ , Times[ coef_, e ] ] :> coef \[Times] 10.^exp


That should take care of the false representation using ReadList without giving the type "Number".

• Returns false since Mathematica transforms my float numbers into something like "integer + float", thus making the list contain pluses. Your function seems awesome, but unfortunately the results will be a little off since the "transformed" floats won't be taking into account. Thanks for your help though! – Gandalf Schmidt Jan 31 '16 at 17:05
• Thanks, this solved everything! – Gandalf Schmidt Jan 31 '16 at 18:18
• Fyi, SemanticImport is not currently reliable for many variable data types, eg dates and times. – alancalvitti Feb 1 '16 at 5:00

I am only a beginner at Mathematica but I notice that Max is left unevaluated if given non numerical data types:

RandomSample[lista,10}

and applying the Head[] function to a few elements which returns the data type