3
$\begingroup$

I am trying to manipulate some large datasets in Mathematica. I have plotted the data, and I would like to be able to easily remove points from the dataset, and it's fairly easy to identify the x-values of the points I need to remove. Let's say I need to remove the data point at an x-value of 1.1. To do this, I could do something like this:

BadData[entry_] := MatchQ[entry, {1.1, _?NumberQ}]
goodData = DeleteCases[data, _?BadData]

However, I have 6 other data sets, all of which I have to remove different outliers (not at 1.1). I'd like to define a function such as

BadData[entry_, bad_] := MatchQ[entry, {bad, _?NumberQ}]

because then for bad I can just put whatever the x-value is of the point I need to remove. I tested the function out by figuring out that the data point with 1.1 as the x-value is the 7th in data, and running

BadData[data[[7]], 1.1]

returns True. So I know the function works the way I want it to, even though I know this might not be the "prettiest" way to do this.

What I can't figure out is how to use this version of baddata as a pattern in DeleteCases because I don't know how to specify the second variable, which would be 1.1 for this set. Is this possible with the setup I have currently?

$\endgroup$
  • 1
    $\begingroup$ Try this: DeleteCases[data, _?(BadData[#, 1.1] &)] $\endgroup$ – Anjan Kumar May 1 at 4:41
3
$\begingroup$

You can use an implicit function more explicitly, as

data = {{1, 5}, {1.1, 6}, {2, 7}};
BadData[entry_, bad_] := MatchQ[entry, {bad, _?NumberQ}]
DeleteCases[data, _?(BadData[#, 1.1] &)]

{{1, 5}, {2, 7}}

$\endgroup$
3
$\begingroup$

You can also define BadData as a pure function:

BadData[bad_] := First[#] == bad&

Then:

DeleteCases[data, _?(BadData[1.1])]

{{1, 5}, {2, 7}}

Another possibility is:

BadData[bad_] := EqualTo[bad] @* First
$\endgroup$

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.