Re: Positions of earliest dates for each month in a list of dates
- To: mathgroup at smc.vnet.net
- Subject: [mg106416] Re: Positions of earliest dates for each month in a list of dates
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 12 Jan 2010 04:47:10 -0500 (EST)
On 1/11/10 at 6:52 PM, warsaw95826 at mypacks.net (Garapata) wrote: >I have a list of 20+ years of dates (from earliest to latest) in >DateList format: >A subset of the list should suffice to illustrate: >myDates = {{1984,12,7,0,0,0}, {1985,1,3,0,0,0},{1985,1,4,0,0,0}, >{1985,1,7,0,0,0} >,{1985,2,1,0,0,0},{1985,2,4,0,0,0},{1985,2,5,0,0,0},{1985,3,4,0,0,0} >, {1985,3,5,0,0,0}, {1985,3,6,0,0,0},...}; >The list does not include all dates, weekends and it may not include >some holidays or other seemingly arbitrary days. >I need to find out the positions of the the first date appearing in >each month. >So, from the sample of myDates above I need to get: >{1, 2, 5, 8} >Corresponding to the positions for the dates: >{{1984,12,7,0,0,0}, {1985,1,3,0,0,0}, {1985,2,1,0,0,0}, >{1985,3,4,0,0,0}} >I've tried working with Position, Split, SplitBy, Ordering, Sort, >and Sortby, but haven't made much progress. Here is one way to achieve what you want using Position, Split and Ordering In[1]:= myDates = {{1984, 12, 7, 0, 0, 0}, {1985, 1, 3, 0, 0, 0}, {1985, 1, 4, 0, 0, 0}, {1985, 1, 7, 0, 0, 0}, {1985, 2, 1, 0, 0, 0}, {1985, 2, 4, 0, 0, 0}, {1985, 2, 5, 0, 0, 0}, {1985, 3, 4, 0, 0, 0}, {1985, 3, 5, 0, 0, 0}, {1985, 3, 6, 0, 0, 0}}; In[2]:= firstDays = First /@ Split[myDates[[Ordering[AbsoluteTime /@ myDates]]], AbsoluteTime[#1[[;; 2]]] == AbsoluteTime[#2[[;; 2]]] &] Out[2]= {{1984, 12, 7, 0, 0, 0}, {1985, 1, 3, 0, 0, 0}, {1985, 2, 1, 0, 0, 0}, {1985, 3, 4, 0, 0, 0}} an alternative to what I've done above would be: firstDays=First/@ Split[SortBy[myDates, AbsoluteTime], AbsoluteTime[#1[[;; 2]]] == AbsoluteTime[#2[[;; 2]]] &] Once you have the first day for each month, finding the position in the original list is easily done by: In[3]:= Flatten[Position[myDates, #] & /@ firstDays] Out[3]= {1,2,5,8}