1
$\begingroup$

How can I combine headers and data to yield desiredResult?

headers = {"heading-1", "heading-2", "heading-3"};

data = Delete[#, 0] & /@ 
   Map[Partition[#, 4] &, 
    Table[Map[#1 <> ToString[i] &, 
      Partition[Alphabet[], 20], {2}], {i, 3}] , {2}];

desiredResult = {{{"heading-1", SpanFromLeft, SpanFromLeft, 
     SpanFromLeft}, {"a1", "b1", "c1", "d1"}, {"e1", "f1", "g1", 
     "h1"}, {"i1", "j1", "k1", "l1"}, {"m1", "n1", "o1", "p1"}, {"q1",
      "r1", "s1", "t1"}}, {{"heading-2", SpanFromLeft, SpanFromLeft, 
     SpanFromLeft}, {"a2", "b2", "c2", "d2"}, {"e2", "f2", "g2", 
     "h2"}, {"i2", "j2", "k2", "l2"}, {"m2", "n2", "o2", "p2"}, {"q2",
      "r2", "s2", "t2"}}, {{"heading-3", SpanFromLeft, SpanFromLeft, 
     SpanFromLeft}, {"a3", "b3", "c3", "d3"}, {"e3", "f3", "g3", 
     "h3"}, {"i3", "j3", "k3", "l3"}, {"m3", "n3", "o3", "p3"}, {"q3",
      "r3", "s3", "t3"}}};

{ {"headers", Grid[{headers}, Frame -> All]}, {"data", 
   Map[Grid[#, Frame -> All] &, data, {1}]}, {"result", 
   Map[Grid[#, Frame -> All] &,  desiredResult]}} // Column
$\endgroup$
2
$\begingroup$
desired = PadRight[#, Automatic, SpanFromLeft]& /@ Join[List /@ List /@ headers, data, 2];

Grid[#, Frame -> All] & /@ desired 

enter image description here

Alternatively,

desired2 = Join[List /@ PadRight[List /@ headers, {3, 4}, SpanFromLeft], data,  2];
desired2 == desired

True

$\endgroup$
  • $\begingroup$ That was quick. Thanks! $\endgroup$ – CalvinDale Sep 5 '18 at 3:21
  • $\begingroup$ @Calvin, you are welcome. Thank you for the accept. $\endgroup$ – kglr Sep 5 '18 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.