My question differs from earlier questions about getting the eom dates from a list; I want to get the position of those dates so I can get the corresponding values from other lists. My problem seems to boil down to one about how to use Position
where pattern is a list. It's an ordinary list of about 5000 dates.
dates[[1 ;; 5]]
{{1995, 1, 31}, {1995, 2, 1}, {1995, 2, 2}, {1995, 2, 3}, {1995, 2, 6}}
As I said, I know how to get the eom dates (there are 229), but I can't seem to make Position
work with a list.
Map[Last, GatherBy[dates, {#[[1]], #[[2]]} &]][[1 ;; 5]]
{{1995, 1, 31}, {1995, 2, 28}, {1995, 3, 31}, {1995, 4, 28}, {1995, 5,31}}
So, here's the kluge. Partition the dates into pairs. Map a function over the list of pairs that returns 1 if the day of the second member is less than the day of the first member, and zero otherwise. Finally, get the position of the members of this list that equal 1.
p = Flatten[Position[If[#[[2,3]]-#[[1, 3]]<0,1,0]&/@ Partition[dates,2,1],1]]
{1, 21, 44, 64, 87, 109, 130, 153, 174, 196, 218, 239,...}
And here is the list of values:
csh[[p]]
{114.141, 115.698, 116.331, 117.328, 119.333, 119.983,...}
It works, but it seems kind of clumsy.
Position
with your first way of finding out the eom dates, you can probably usePosition[dates,Or@@(that expression)]
. $\endgroup$ – C. E. Feb 17 '14 at 22:35