Message Boards Message Boards

0
|
13929 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

[?] Join data with Dates?

Posted 7 years ago

Dear All,

I got a simple question. How to join data from two different files with the same Dates?

Data1 = {
   {{2006, 01, 01}, 27},
   {{2006, 01, 02}, 25},
   {{2006, 01, 03}, 86},
   {{2006, 01, 04}, 72},
   {{2006, 01, 05}, 66},
   {{2006, 01, 06}, 25},
   {{2006, 01, 07}, 13}
   };
Data2 = {
   {{2006, 01, 01}, 33},
   {{2006, 01, 02}, 36},
   {{2006, 01, 03}, 100},
   {{2006, 01, 04}, 100},
   {{2006, 01, 05}, 76},
   {{2006, 01, 06}, 100},
   {{2006, 01, 07}, 27}};

This should be the outcome:

HowToJoinData1withData2 =
  {{{2006, 01, 01}, 27, 33},
   {{2006, 01, 02}, 25, 36},
   {{2006, 01, 03}, 86, 100},
   {{2006, 01, 04}, 72, 100},
   {{2006, 01, 05}, 66, 76},
   {{2006, 01, 06}, 25, 100},
   {{2006, 01, 07}, 13, 27}};

Please can you help? Regards,.....Jos

POSTED BY: Jos Klaps
5 Replies

The (N+1)th solution:

{#[[1, 1]], Sequence @@ #[[All, 2]]} & /@ GatherBy[data1~Join~data2, First]
POSTED BY: Henrik Schachner

To All,

Thanks for your quick reply and excellent support. This was actually what I am looking for !

Best Regards,....Jos

POSTED BY: Jos Klaps
Normal[GroupBy[Join[data1, data2], First -> Last, 
       Sequence @@ # &]] /. {Rule -> List}


SplitBy[Sort[Join[data1, data2]], First] /. 
 z : {{_List, _Integer} ..} :> {First[First[z]],  Sequence @@ z[[All, 2]]}

lots of ways ;-)

POSTED BY: David Reiss

Please try

Map[Function[FlattenAt[#, 2]], 
 ReplaceAll[
  Normal[Merge[
    Map[Function[Association[Rule[#[[1]] , #[[2]]]]], 
     Join[Data1, Data2]], Identity]] , RuleDelayed[Rule, List]] ]

If your data fits this pattern {date, value} if it is {date,values,...} then change Rule[#[[1]],#[[2]]] to Rule[First[#],Rest[#]] and modify the rest of the code accordingly to get the final list into the form you desire.

POSTED BY: Hans Michel

You could try:

Merge[{Rule@@@Data1,Rule@@@Data2},Identity]
List@@@Normal[%]
Prepend[#2,#1]&@@@%
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract