3
$\begingroup$

Basically I want to make a histogram with a list of dates by using DateHistogram[]. However I have the data of the dates in nested list format, with elements like {2020,6,26}, and although I know how to convert it to a date by using DateObject[{2020,6,26}] I can not construct a list of dates by iteration. I am trying the following:

For[i = 0, i < 5, i++, Insert[egunak, DateObject[watchdates[[i]]], 1]]

where watchdates is the nested list of {2020,6,26} date elements and egunak is the list I want to create with the dates (I have tried it to define it before).

I have also tried using Append instead of Insert or by iterating with Do, but didn't succeed. I am pretty new with the Wolfram language, sorry.

$\endgroup$
  • 1
    $\begingroup$ Is egunak = watchdates[[;;4]] what you are looking for? $\endgroup$ – Edmund Jun 26 at 22:31
  • $\begingroup$ Be sure to check out AppendTo and compare it with Append. Also, Do is better than For because it's less error prone, but functional solutions like Map is the best of all in almost all situations. (Easier to read and has better performance.) $\endgroup$ – C. E. Jun 27 at 8:43
  • $\begingroup$ I see, alright thanks a lot!! $\endgroup$ – Andoni Royo Jun 28 at 15:54
6
$\begingroup$

You can Map the DateObject[] function onto each member of your watchdates list like in

Map[DateObject, {{2020, 6, 26}, {2020, 6, 25}}]

which yields a list of DateObjects.

There is also a shorter symbolic form for Map

DateObject /@ {{2020, 6, 26}, {2020, 6, 25}}

Is this what you had in mind?

| improve this answer | |
$\endgroup$
  • $\begingroup$ Yeeees!! that exactly what I wanted. I didn't know about the Map function $\endgroup$ – Andoni Royo Jun 26 at 23:24

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.