Skip to main content
Tweeted twitter.com/StackMma/status/938955902866075648
edited title
Link
Hedgehog
  • 624
  • 3
  • 15

Replace sublist entries after first the first match (and retain plot-ability)

Source Link
Hedgehog
  • 624
  • 3
  • 15

Replace sublist entries after first the match (and retain plot-ability)

I think I have a subtle problem. I have nested lists. The first level can be considered as a time series. The lowest level list is a pair of numbers.
These number s can all be thought of as random. Each number in the pair represents a variable. I am only interested in adjusting the second number/variable in the pair.

Example: Two variables, three time series, four observed pairs in each series:

lst={
{{100., 20.}, {99.7401, 19.7457},{99.7401, 19.7457},{99.7401, -19.7457}},
{{102., 22.}, {98.7401, -39.7457},{98.7401, 39.7457},{98.7401, 39.7457}},
{{103., 21.}, {97.7401, 49.7457},{97.7401, -49.7457},{97.7401, -49.7457}},
}

Objective: I would like to be able to plot each variable as a line plot. I would like to prevent any observation of the second variable from plotting after it takes a negative value (I don't want to just hide that region of the plot).

That is I would like to keep these NULL or empty list entries so that in other parts of the work flow I can calculate the number of observations removed, the value prior to the removed observation, etc. etc. This suggests replacing the data rather than delete the data. The result, I think I need is, the three time series returned as:

res={
{{100., 20.}, {99.7401, 19.7457},{-99.7401, 19.7457},{99.7401, NULL}},
{{102., 22.}, {98.7401, NULL},{98.7401, NULL},{98.7401, NULL}},
{{103., 21.}, {-97.7401, 49.7457},{-97.7401, NULL},{97.7401, NULL}},
}

I've tried several things. One approach seemed to be find the position of the negatives and then do something with those index values. I can get Position to return the full position, I'm at a loss about the best way to do next step, replace all entries after the first negative.

Position[lst, _?(# < 0 &)]
{{1, 4, 2}, {2, 2, 2}, {3, 3, 2}, {3, 4, 2}}

Appreciate any assistance.