2
$\begingroup$

Here a base date list

lstBase={"2018-03-01","2018-03-02","2018-03-03","2018-03-04","2018-03-07","2018-03-08","2018-03-11","2018-03-12","2018-03-13"};

and a date-value list

lstValue={{"2018-03-01",10},{"2018-03-02",12},{"2018-03-07",23},{"2018-03-13",84}};

How to make a new date-value list, and it shall be like this,

lstValueNew={{"2018-03-01",10},{"2018-03-02",12},{"2018-03-03",12},{"2018-03-04",12},{"2018-03-07",23},{"2018-03-08",23},{"2018-03-11",23},{"2018-03-12",23},{"2018-03-12",84}};

Thanks!

$\endgroup$
1
2
$\begingroup$

Here's one in the vein of Chris Degnen's, but using Association and Replace instead of Increment:

lstBase = {"2018-03-01", "2018-03-02", "2018-03-03", "2018-03-04", 
   "2018-03-07", "2018-03-08", "2018-03-11", "2018-03-12", 
   "2018-03-13"};
lstValue =
  AssociationThread @@ 
   Transpose@{{"2018-03-01", 10}, {"2018-03-02", 12}, {"2018-03-07", 
      23}, {"2018-03-13", 84}};

Module[{cached},
 MapThread[
  {#, Replace[#2, {i : Except[_Missing] :> Set[cached, i], _Missing :>
        cached}]} &,
  {lstBase, Lookup[lstValue, lstBase]}
  ]
 ]

{{"2018-03-01", 10}, {"2018-03-02", 12}, {"2018-03-03", 
  12}, {"2018-03-04", 12}, {"2018-03-07", 23}, {"2018-03-08", 
  23}, {"2018-03-11", 23}, {"2018-03-12", 23}, {"2018-03-13", 84}}
$\endgroup$
1
  • $\begingroup$ Thanks! It works using list-operations. $\endgroup$ – Jerry Mar 26 '18 at 9:37
2
$\begingroup$
f[b_, v_] := Module[{p = 1, x = Null},
  (If[# == v[[p, 1]], x = v[[p++, 2]]];
     {#, x}) & /@ b]

lstValueNew = f[lstBase, lstValue]
{{"2018-03-01", 10}, {"2018-03-02", 12}, {"2018-03-03", 12},
 {"2018-03-04", 12}, {"2018-03-07", 23}, {"2018-03-08", 23},
 {"2018-03-11", 23}, {"2018-03-12", 23}, {"2018-03-13", 84}}
$\endgroup$
5
  • $\begingroup$ Thanks! but variable x be itself in other testing-case. $\endgroup$ – Jerry Mar 26 '18 at 9:39
  • $\begingroup$ @Jerry x now isolated as local variable. $\endgroup$ – Chris Degnen Mar 26 '18 at 10:33
  • $\begingroup$ the x appears in the result as “x$21720” $\endgroup$ – Jerry Mar 26 '18 at 12:00
  • $\begingroup$ @Jerry I guess your working data has dates in lstBase earlier than the first one in lstValue. Those dates will get a Null value with the above edit. $\endgroup$ – Chris Degnen Mar 26 '18 at 14:44
  • $\begingroup$ Yes! the cute code works on the given lists, I hope it also work on this complicated testing-lists raw.githubusercontent.com/LKTPD/Testcasemma/master/… $\endgroup$ – Jerry Mar 27 '18 at 1:11
1
$\begingroup$

One could use TimeSeries and an appropriate resampling:

ts = TimeSeries[{DateObject[#[[1]]], #[[2]]} & /@ lstValue, 
  ResamplingMethod -> {"Interpolation", InterpolationOrder -> 0}]

lstValueNew = With[{d = DateObject[#]}, {#, ts[d]}] & /@ lstBase
(* {{"2018-03-01", 10}, {"2018-03-02", 12}, {"2018-03-03", 12},
   {"2018-03-04", 12}, {"2018-03-07", 23}, {"2018-03-08", 23}, 
   {"2018-03-11", 23}, {"2018-03-12", 23}, {"2018-03-13", 84}} *)
$\endgroup$
2
  • $\begingroup$ TemporalData objects have the advantage of specifying an interpolation option between time points. This pays big efficiency dividends with multiple sparse time series objects that need subsequent arithmetic. $\endgroup$ – Mitch Mar 26 '18 at 6:53
  • $\begingroup$ Thanks! It works! $\endgroup$ – Jerry Mar 26 '18 at 9:35

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.