I have a list of dates, strings and integers:
lis = {{DateObject[{2000,1,1}],"a",1,"w"},{DateObject[{1999,12,31}],"a",1,"x"},{DateObject[{1999,12,31}],"a",2,"z"}
I would like to identify pairs of elements of lis that have identical values in their 2nd and 3rd position (in this case, "a" and 1), and then delete the older element of the pair, to leave:
res = {{DateObject[{2000,1,1}],"a",1,"w"},{DateObject[{1999,12,31}],"a",2,"z"}}
I realize this is likely be a duplicate question, but can't seem to find it; thanks for any thoughts.
Answer
You can do this by first Sorting the list by the date (the First element), reversing the order (so that we have newer first), then using DeleteDuplicatesBy to indicate the second and third column.
DeleteDuplicatesBy[Reverse@SortBy[lis, First], #[[{2, 3}]] &]
Comments
Post a Comment