2
$\begingroup$

I wish to convert Data to Data2. Both sets are provided below. Note that the second set has an arbitrary parameter "t". How can this be done.

Data = {{{2013, 1, 1}, 49.90611}, {{2013, 1, 2}, 
   50.33952}, {{2013, 1, 3}, 50.75688}, {{2013, 1, 4}, 
   50.7649}, {{2013, 1, 7}, 50.24321}, {{2013, 1, 8}, 49.256}, 
     {{2013, 1, 9}, 49.28008}, {{2013, 1, 10}, 
   49.44863}, {{2013, 1, 11}, 49.26403}, {{2013, 1, 14}, 
   49.46468}, {{2013, 1, 15}, 49.56099}, {{2013, 1, 16}, 49.9944}}



Data2 = {{t, 49.90611}, {t + 1, 50.33952}, {t + 2, 50.75688}, {t + 3, 
   50.7649}, {t + 4, 50.24321}, {t + 5, 49.256}, {t + 6, 
   49.28008}, {t + 7, 49.44863}, {t + 8, 49.26403}, 
     {t + 9, 49.46468}, {t + 10, 49.56099}, {t + 11, 49.9944}}    
$\endgroup$
1
  • $\begingroup$ Where did you get stuck ? $\endgroup$ Aug 26 '15 at 1:08
2
$\begingroup$
Data2=MapThread[{t + #1, Last@#2} &, {Range[0, Length@Data - 1], Data}]

{{t, 49.9061}, {1 + t, 50.3395}, {2 + t, 50.7569}, {3 + t, 50.7649}, {4 + t, 50.2432}, {5 + t, 49.256}, {6 + t, 49.2801}, {7 + t, 49.4486}, {8 + t, 49.264}, {9 + t, 49.4647}, {10 + t, 49.561}, {11 + t, 49.9944}}

$\endgroup$
3
$\begingroup$

Works if dates aren't sequential days:

Data2 = Thread[{t + QuantityMagnitude@DateDifference[Data[[1, 1]], #] & /@ Data[[All, 1]], Data[[All, 2]]}]

(Not sure if that's what you were going for, if not...)

Data2 = Thread[{t + Range[0, Length@Data - 1], Data[[All, 2]]}]
$\endgroup$
2
  • $\begingroup$ @kala Yes, these inputs are good alternatives, flexible. $\endgroup$
    – thils
    Aug 26 '15 at 1:26
  • 1
    $\begingroup$ @thils Note that the first option calculates number of DAYS from first data point and adds t. Again, not sure if that's what you were after, but coupled with @image_doctor's answer, you have a couple good choices. $\endgroup$
    – kale
    Aug 26 '15 at 1:27

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.