0
$\begingroup$

My problem should be a trivial one but for the life of me I can't get it to work.

This is my data (10 rows, each made up of a DateList and a list of 5 numeric values corresponding to that date);

list = {{{2008, 1, 2, 14, 31, 0}, {36.67, 36.69, 36.61, 36.65, 
   20800}}, {{2008, 1, 2, 14, 32, 0}, {36.68, 36.71, 36.68, 36.68, 
   2600}}, {{2008, 1, 2, 14, 33, 0}, {36.71, 36.71, 36.67, 36.67, 
   1700}}, {{2008, 1, 2, 14, 34, 0}, {36.68, 36.72, 36.68, 36.69, 
   700}}, {{2008, 1, 2, 14, 35, 0}, {36.7, 36.72, 36.66, 36.66, 
   2800}}, {{2008, 1, 2, 14, 36, 0}, {36.68, 36.68, 36.64, 36.66, 
   6200}}, {{2008, 1, 2, 14, 37, 0}, {36.68, 36.68, 36.66, 36.67, 
   2900}}, {{2008, 1, 2, 14, 38, 0}, {36.66, 36.67, 36.63, 36.67, 
   1400}}, {{2008, 1, 2, 14, 39, 0}, {36.67, 36.68, 36.65, 36.68, 
   2500}}, {{2008, 1, 2, 14, 40, 0}, {36.67, 36.68, 36.63, 36.68, 
   13000}}}

Let's say I want to generate a new list and extract just the minute value from each row and the corresponding 1st data value. This is how I thought I should do it;

list/.{{year_, month_, day_, hour_, min_, sec_}, {open_, high_, low_, close_, vol_}} -> {min, open}

However, this produces the following unexpected (for me) result.

{{{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.67}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.68}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.71}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.68}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.7}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.68}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.68}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.66}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 
  36.67}, {{31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, 36.67}}

The replacement rule has correctly identified the minute column and the open column, which is what I want. But it has replicated all the minutes in the entire dataset for each row as well as wrapped an additional set of braces around them, which I cannot understand why. It is as if minute is matching on the entire column and open only on the individual datum (which is the behaviour I want). In other words, the final result I seek is;

{{31,36.67}, {32,36.68}, {33,36.71}, {34,36.68}, {35,36.7}, {36,36.68}, {37,36.68}, {38,36.66}, {39,36.67}, {40,36.67}}

Does anyone have any ideas as to the error I have made?

$\endgroup$
6
  • $\begingroup$ the rule is a syntax error (really I can't even C&P it into a notebook); you're missing a closing } after vol_. $\endgroup$
    – Pillsy
    Commented Mar 20, 2020 at 19:14
  • $\begingroup$ Thanks, that was a typo in my post only - the rule is entered correctly in my notebook that is producing the result as above. $\endgroup$ Commented Mar 20, 2020 at 19:19
  • $\begingroup$ try replacing -> with :>? $\endgroup$
    – kglr
    Commented Mar 20, 2020 at 19:22
  • $\begingroup$ @kglr That worked, thanks! But what's the rationale? Why should the application of the rule be delayed? $\endgroup$ Commented Mar 20, 2020 at 19:32
  • $\begingroup$ see RuleDelayed >> Properties and Relations and Rule >> Properties and Relations for an explanation. $\endgroup$
    – kglr
    Commented Mar 20, 2020 at 19:55

0

Browse other questions tagged or ask your own question.