MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Positions of earliest dates for each month in a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106414] Re: [mg106404] Positions of earliest dates for each month in a list
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Tue, 12 Jan 2010 04:46:48 -0500 (EST)
  • Reply-to: hanlonr at cox.net

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}};

If the dates are not sorted then sort first. Then, either

Flatten[Position[myDates, #] & /@
  Union[myDates,
   SameTest -> (Take[#1, 2] == Take[#2, 2] &)]]

{1,2,5,8}

Or

Flatten[Position[myDates, #[[1]]] & /@
  SplitBy[myDates, #[[2]] &]]

{1,2,5,8}


Bob Hanlon

---- Garapata <warsaw95826 at mypacks.net> 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.

I've also tried creating an index for the dates like this:

SplitBy[Transpose[Join[{myDates}, {Range[1, Length[myDates]]}]]];

and hoped to use its output to sort or split the list into sublists of
each month then just use Part to take the first date in each month
with its paired index, but I still can't figure out how to do it.

I've got the feeling I've missed a simple solution to this, any help
much appreciated.



  • Prev by Date: syntax extension
  • Next by Date: Re: changing text style in legends in 7.0
  • Previous by thread: Re: syntax extension
  • Next by thread: Program Generation of Mathematica Code