Search code examples
arraysexcelsortingexcel-formulapivot-table

How do you combine multiple column pairs into one table (with zeros for missing values)?


I have many column pairs (mass spectral data) that I want to combine into one array with the zeros for the missing rows.

I have something like this:

column pairs

Mass 10 Mass 11 Mass 12 Mass 13
80 22005 81 30908 81 46532 80 22259
81 33306 82 47792 82 97559 81 42002
82 27314 84 1315498 83 35698 82 233130
83 27204 85 110460 84 2391605 84 6892485
84 644196 86 25905 86 51365 85 502763
85 54723 87 31240 87 34415 86 37660
86 28384 88 22801 88 21819 87 40078
87 32212 90 24878 89 25326 88 39850
88 38615 91 36830 90 30998 89 44535
89 13155 92 27189 92 28916 90 38188
90 20406 93 29146 93 19224 92 31855
95 22505 94 31359 93 18951
96 18047 95 22533 94 37879
97 97665 96 28238 95 38750
97 132143 96 16119
98 18459 97 171050
99 17121 98 23113
101 9579 99 34733
102 235454 100 23821
103 25700 101 28792
104 17415 102 442859
105 28789 103 28505
106 16005 104 14448
107 17534 106 22950
108 24548 107 25922
108 29475
109 15177
110 123036
111 30295
112 18465
113 52162
114 19906
115 64107
116 60852

...and I want to have something like this:

combined array

Mass 10 11 12 13
80 22005 0 0 22259
81 33306 30908 46532 42002
82 27314 47792 97559 233130
83 27204 0 35698 0
84 644196 1315498 2391605 6892485
85 54723 110460 0 502763
86 28384 25905 51365 37660
87 32212 31240 34415 40078
88 38615 22801 21819 39850
89 13155 0 25326 44535
90 20406 24878 30998 38188
91 0 36830 0 0
92 0 27189 28916 31855
93 0 29146 19224 18951
95 0 22505 22533 38750
96 0 18047 28238 16119
97 0 97665 132143 171050
94 0 0 31359 37879
98 0 0 18459 23113
99 0 0 17121 34733
101 0 0 9579 28792
102 0 0 235454 442859
103 0 0 25700 28505
104 0 0 17415 14448
105 0 0 28789 0
106 0 0 16005 22950
107 0 0 17534 25922
108 0 0 24548 29475
100 0 0 0 23821
109 0 0 0 15177
110 0 0 0 123036
111 0 0 0 30295
112 0 0 0 18465
113 0 0 0 52162
114 0 0 0 19906
115 0 0 0 64107
116 0 0 0 60852

I tried using the pivot column, but it did not work. Some TOCOL usage may be an option.


Solution

  • Transform Data

    =LET(data,A1:H11,na,0,
        s,SEQUENCE(COLUMNS(data)/2,,,2),
        sh,TAKE(data,1),
        dh,HSTACK(TAKE(sh,,1),CHOOSECOLS(sh,s+1)),
        sd,DROP(data,1),
        drl,SORT(UNIQUE(TOCOL(CHOOSECOLS(sd,s),1))),
        dd,DROP(REDUCE("",s,LAMBDA(rr,c,
            HSTACK(rr,XLOOKUP(drl,CHOOSECOLS(sd,c),
                CHOOSECOLS(sd,c+1),na)))),,1),
        VSTACK(dh,HSTACK(drl,dd)))
    

    The Variables

    s   - {1;3;5;7}
    s+1 - {2;4;6;8}
    sh  - A1:H1
    sd  - A2:H11
    dh  - J1:N1
    drl - J2:J17
    dd  - K2:N17
    

    enter image description here

    Mass 11 Mass 12 Mass 13 Mass 14
    21 5381 24 6004 24 3391 19 4477
    14 5939 21 8932 26 7270 13 7440
    23 1855 25 7809 15 7419
    25 8175 23 9400 17 1035
    19 3749 24 6855
    17 4518 14 4876 21 3810
    28 9400 13 8150
    16 5418 23 3158
    18 4676 21 9707
    11 4962 10 9057 20 6584