# How to select the data in the following way?

I have the data which is obtained by joining the subsets with 12 rows. The subset has the following form:

subset = {{…},{2345678,...},{2345678,...},{13,1,5...},{-13,1,5,...},{13,1,6,...},{-13,1,6,...},{…}}


In the subset may exist other rows with the first element equal to 13 or -13, and these rows are among the last rows of the subset. They differ from each other by the value of the third element. For the other rows with the first element equal to 13 or -13 the third element is 3.

I need to remove from the subset the second row with 2345678 and the rows with the first element equal to 13 or -13 AND the third element equal to 6.

Could you please tell me how to do this?

An example of the data is

data = {{2, -1, 0, 0, 501, 0, 0, 0, 245.072, 245.072, 0, 0, -1}, {-1, -1, 0,
0, 0, 501, 0, 0, -127.298, 127.298, 0, 0, 1}, {24, 2, 1, 2, 0, 0,
70.9655, -78.0553, -47.2883, 139.991, 78.9464, 0, 0}, {25, 2, 1, 2,
0, 0, -70.9655, 78.0553, 165.063, 232.379, 125.002, 0, 0}, {2345678,
2, 4, 4, 0, 0, -2.68444, 34.8928, -12.5353, 37.2584, 2.51808, 0,
0}, {2345678, 2, 4, 4, 0, 0, -68.281, 43.1626, 177.598, 195.121,
2.38498, 0, 0}, {-13, 1, 5, 5, 0, 0, -0.930723, 22.6832, -9.05917,
24.443, 0, 0, 1}, {13, 1, 5, 5, 0, 0, -1.75372, 12.2096, -3.47615,
12.8154, 0, 0, 1}, {-13, 1, 6, 6, 0, 0, -36.0541, 23.0327, 91.2242,
100.758, 0, 0, 1}, {13, 1, 6, 6, 0, 0, -32.2269, 20.1299, 86.374,
94.3623, 0, 0, 1}, {-11, 1, 3, 3, 0, 0, -7.89361, -27.836, -35.4404,
45.7513, 0, 0, 1}, {12, 1, 3, 3, 0, 0, 78.8591, -50.2193, -11.8478,
94.2396, 0, 0, -1}, {-1, -1, 0, 0, 0, 501, 0, 0, 239.409, 239.409,
0, 0, 1}, {1, -1, 0, 0, 501, 0, 0, 0, -101.226, 101.226, 0,
0, -1}, {23, 2, 1, 2, 0, 0, 78.8375, -33.3418, 141.353, 189.098,
91.926, 0, 0}, {25, 2, 1, 2, 0, 0, -78.8375, 33.3418, -3.17023,
151.536, 125.005, 0, 0}, {2345678, 2, 4, 4, 0, 0, -6.5774, 67.8729,
13.7657, 69.581, 1.42341, 0, 0}, {2345678, 2, 4, 4, 0,
0, -72.2601, -34.5311, -16.9359, 81.9554, 3.99437, 0, 0}, {-13, 1,
5, 5, 0, 0, -0.330111, 1.85053, 0.199115, 1.89026, 0, 0, 1}, {13, 1,
5, 5, 0, 0, -6.24729, 66.0224, 13.5666, 67.6907, 0, 0, 1}, {-13, 1,
6, 6, 0, 0, -52.0397, -25.9628, -10.771, 59.1457, 0, 0, -1}, {13,
1, 6, 6, 0, 0, -20.2203, -8.56831, -6.1649, 22.8097, 0, 0, -1}, {13,
1, 3, 3, 0, 0, 81.3055, -48.0497, 73.6311, 119.753, 0,
0, -1}, {-13, 1, 3, 3, 0, 0, -2.46804, 14.7079, 67.7221, 69.3447, 0,
0, 1}, {-3, -1, 0, 0, 0, 501, 0, 0, 167.828, 167.828, 0, 0,
1}, {4, -1, 0, 0, 501, 0, 0, 0, -734.964, 734.964, 0, 0, -1}, {24,
2, 1, 2, 0, 0, -120.631, -25.6225, 121.935, 191.696, 81.6757, 0,
0}, {25, 2, 1, 2, 0, 0, 120.631, 25.6225, -689.071, 711.095,
125.017, 0, 0}, {2345678, 2, 4, 4, 0, 0, 33.9448, 47.2155, -481.65,
485.247, 9.83546, 0, 0}, {2345678, 2, 4, 4, 0, 0,
86.6858, -21.5929, -207.421, 225.848, 1.76328, 0, 0}, {-13, 1, 5, 5,
0, 0, 20.8163, 36.3766, -350.561, 353.057, 0, 0, 1}, {13, 1, 5, 5,
0, 0, 13.1285, 10.8389, -131.089, 132.19, 0, 0, 1}, {-13, 1, 6, 6,
0, 0, 63.0195, -16.5053, -151.212, 164.647, 0, 0, 1}, {13, 1, 6, 6,
0, 0, 23.6664, -5.08762, -56.21, 61.2008, 0, 0, 1}, {-13, 1, 3, 3,
0, 0, -12.2357, -9.84504, 67.6429, 69.4421, 0, 0, 1}, {14, 1, 3, 3,
0, 0, -108.395, -15.7775, 54.2925, 122.254, 0, 0, -1}, {4, -1, 0, 0,
501, 0, 0, 0, 238.749, 238.749, 0, 0, -1}, {-3, -1, 0, 0, 0, 501,
0, 0, -44.9598, 44.9598, 0, 0, 1}, {24, 2, 1, 2, 0, 0,
4.95992, -7.38008, 89.2702, 120.528, 80.4912, 0, 0}, {25, 2, 1, 2,
0, 0, -4.95992, 7.38008, 104.519, 163.181, 124.998, 0, 0}, {2345678,
2, 4, 4, 0, 0, 32.2085, -37.3979, 90.1023, 103.165, 9.40968, 0,
0}, {2345678, 2, 4, 4, 0, 0, -37.1684, 44.778, 14.4171, 60.0161,
2.74152, 0, 0}, {-13, 1, 5, 5, 0, 0, 11.0037, -7.09154, 27.3773,
30.3461, 0, 0, -1}, {13, 1, 5, 5, 0, 0, 21.2048, -30.3064, 62.7251,
72.8187, 0, 0, -1}, {-13, 1, 6, 6, 0, 0, -15.6595, 17.7405, 7.03582,
24.687, 0, 0, -1}, {13, 1, 6, 6, 0, 0, -21.5089, 27.0376, 7.38129,
35.3291, 0, 0, -1}, {-11, 1, 3, 3, 0, 0, -26.9091, 3.57243, 2.2821,
27.241, 0, 0, 1}, {12, 1, 3, 3, 0, 0, 31.8691, -10.9525, 86.9881,
93.2873, 0, 0, -1}}


You can delete the second row that matches {2345678, ___} using a combination of Delete and Position and using DeleteCases[{13|-13,_,6,___}] on the resulting list:

DeleteCases[{13|-13,_,6,___}] @ Delete[data, Position[data, {2345678, ___} ][[2]]]


To package it into a function (that also checks that input data has the specified position to delete):

ClearAll[f0]
f0[pattern1_, {patttern2_, position_}] := DeleteCases[pattern1] @
Delete[#, If[Length @ # >= position, #[[2]], {}] & @ Position[#, pattern2 ]] &;

f0[{13 | -13, _, 6, ___}, {{2345678, ___}, 2}] @ data;


Does DeleteCases[data, {13 | -13, _, 6, ___} | {2345678, ___}] do what you want?

Edit: After having read your comment, I think you could be looking for the command GatherBy.

If you use GatherBy[data,#[[1]]&] you get lists in which all sublists are collected that have the same first element. You could then use something like

GatherBy[data,#[[1]]&][[;;,1]]


to select only the first sublist from each set.

Or, if the first two elements have to agree in order to be considered equal, you could use

GatherBy[data,#[[{1,2}]]&][[;;,1]]


Of course, you can apply any transformation to the data before it is being considered for comparison. For example, if you want to consider only the first two elements and apply the absolute value to the first element (to make 13 and -13) the same for the comparison, you could use

GatherBy[data,{Abs[#[[1]]],#[[2]]}&][[;;,1]]


Does this come closer to what you have in mind?

• This command removes all the rows with 2345678. I need to keep the first row with this element and remove only the second row. – John Taylor Jul 2 '19 at 10:10
• I need to do this for each subset. – John Taylor Jul 2 '19 at 10:15