0
$\begingroup$

I have the following lists:

id = {5, 11, 17, 24, 25, 31, 32, 33, 37, 38, 39, 40}

values = {0.00390625, 0.00292969, 0.00769043, 0.00350189, 0.316406,0.00787926, 0.237305, 0.207642, 0.0118189, 0.0945511, 0.0709133, 0.0354567}

a = {5., 7., 8., 10., 5., 11., 7., 8., 12., 10., 11., 12.}

b = {-4., -4., -3., -3., 4., -2., 4., 3., -1., 3., 2., 1.}

which can be visualized below. Also shown is what I wish to achieve.

I need to split the data in two: One part where b is negative, and one part where b is positive. Moreover, each of the two parts obtained are finally sorted in ascending order by a (or aneg and apos in the wording of the image).

What is a good approach to this problem?

enter image description here

$\endgroup$
2
$\begingroup$
{neg, pos} = 
 SortBy[#, #[[3]] &] & /@ 
  SplitBy[SortBy[Transpose[{id, values, a, b}], Last], Sign[#[[4]]]&]

TableForm /@ {neg, pos}

enter image description here

$\endgroup$
  • $\begingroup$ this gives an error for me: Part::partw: Part 4 of #1 does not exist. Set::shape: Lists {neg,pos} and {{{5,0.00390625,5.,-4.}},{{11,0.00292969,7.,-4.}},{{17,0.00769043,8.,-3.}},{{24,0.00350189,10.,-3.}},{{31,0.00787926,11.,-2.}},{{37,0.0118189,12.,-1.}},{{40,0.0354567,12.,1.}},{{39,0.0709133,11.,2.}},{{33,0.207642,8.,3.}},{{38,0.0945511,10.,3.}},{{25,0.316406,5.,4.}},{{32,0.237305,7.,4.}}} are not the same shape. $\endgroup$ – user120911 Dec 2 '18 at 21:30
  • $\begingroup$ @user120911 sorry, in the copy/paste process I missed the last three symbols of the code :) fixed now. $\endgroup$ – Fraccalo Dec 3 '18 at 7:03

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.