Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have several files, and I want to extract for each of them a table, and then select just numbers, and keep only rows with five elements.

My code works fine during import

data = Import[#, "Table"] & /@     FileNames["*.txt",
NotebookDirectory[]] ; (*This line import only tables of numerical
values from txt files*)

but if I want to apply selection of numbers to my list of tables, simply doesn't work,

Select[#, NumberQ[#] & ] & /@ data

and I have to specify a level to make it work,

Select[#, NumberQ[#] & ] & /@ data[[1]]

and I am supposed to automate this for twelve tables, rather that typing for each element. Any suggestions what I am doing wrong? I think that my list of tables needs to be specified different, but I don't know how.

share|improve this question
You indeed have to indicate a level to make it work, annd you can do that with Map[foo, table, level] instead of foo/@table. – Wouter Jun 2 '14 at 15:54
up vote 2 down vote accepted

When working with nested lists, you should check what part of the lists your mapped functions "see". The pure function with Select in

Select[#, ...]& /@ data

will get data[[1]] as an argument (and data[[2]], data[[3]], and so on). Type data[[1]] into the notebook and look what's inside. It is the entire Table imported from one of your .txt files.

You said however, you want to select rows. So what you want is a) go through every file, and then for every file b) go through every row, and Select those matching your cirteria. Thus, you will have two levels of mapping:

Select[#, ...]& /@ #& /@ data

In case you get confused with the meaning of all these #'es, you can also use functions with named arguments instead of pure functions:

Function[{table}, Select[#, ...]& /@ table] /@ data

Note that you do not append a & here because Function is used (& is the short form of Function). Functions with named arguments are particularly useful if you wanted to access table from inside your Select function. If you only had #, you can only access the argument of the innermost pure function.

Finally, you can make it shorter by using Map explicitly instead of /@ and specifiy that the function is to be mapped one level deeper, i.e. level 2 instead of 1:

Map[Select[#, ...]&, data, {2}]

Note the Mathematica level specification {2} which means "only level 2".

I hope I could make this more clear. If you are unclear about any of the italic words, please look them up in the documentation. :)

share|improve this answer
Yes it works the Select[#, ...]& /@ #& /@ data the two level of specification. – Kloudpaper Jun 4 '14 at 9:03

Your Answer


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.