I'm importing some data from a file. However, the file was created by an unreliable instrument, such that the data looks like this:

in: data = Import["D:\\6000s.txt", "Table"]  
out: {...{5,0,46.4109},{6,0,46.6017},{7,0,No,Data},{8,0,45.7324}...}

The lines containing "No Data" can be safely deleted, but I don't know how to do this well. How can I efficiently map over my table and erase all rows that contain a string?

Thanks in advance for your help.

Edit: An example of my data is here . Also, a closely related question is here

Edit 2: @RunnyKine's and @MikeHoneyChurch's answers in the comments did the trick. I ended up doing the following:

data = Import["my_data.txt", "Table"];
data = DeleteCases[data, {__, _String, _String}]

This is much faster than the ugly for loops I had used previously. Thanks for your help!

  • 1
    $\begingroup$ Can you give a sample of the file or a link to the actual file? A better way to read the file than using Import might be available. $\endgroup$ – RunnyKine Oct 30 '13 at 20:53
  • $\begingroup$ Sure thing, @RunnyKine. I edited the question to include a link to an example datafile. $\endgroup$ – avn2109 Oct 30 '13 at 21:07
  • $\begingroup$ If that is the structure then DeleteCases[yourData,{__,No Data}] and replace No Data with what is present e.g. a Null, empty string ... $\endgroup$ – Mike Honeychurch Oct 30 '13 at 21:08
  • $\begingroup$ I think for your case, since you've successfully imported the data, doing what @MikeHoneychurch suggested is the way to go. But be careful since there's no list of the form {__, No Data} instead it should be DeleteCases[data, {__, No, Data} or just DeleteCases[data, {__, No, _}] $\endgroup$ – RunnyKine Oct 30 '13 at 21:19
  • $\begingroup$ Welcome to Mathematica.SE, @avn2109! May I suggest actually posting the solution as an answer? That way people will know that this question already has an answer. $\endgroup$ – Verbeia Oct 30 '13 at 22:12

For many cases the suggested answer will work as desired. But the code using data most probably will expect a purely numerical array, probably even with a fixed length. I have found it to be more reliable in such cases to select those entries that match the expected pattern than to delete every particular form of rows not matching it. That is I'd rather suggest to use something like




or even:

Cases[data, {_?NumericQ, _?NumericQ, _?NumericQ}]
Select[data, (VectorQ[#, NumericQ] && Length[#] == 3) &]

it might help to avoid troubleshooting the same thing again just because the instrument starts to report (other) errors in a different notation...

| improve this answer | |
  • $\begingroup$ That's less brittle and seems like a best practice in general. $\endgroup$ – avn2109 Nov 4 '13 at 23:37

This is just to get an answer on record so the question can be removed from not-answered list.

data = DeleteCases[Import["my_data.txt", "Table"], {__, _String, _String}]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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