# Query in Lists Mathematica - Pivot table

Recently, I finished a simulation, and I got the following data:

{{1.1, 11, 0.00107393, 0.0988187},
{1.1, 12, 0.00172679, 0.173259},
{1.1, 13, 0.0022463, 0.236371},
{1.1, 14, 0.00267865, 0.290542},
(...)
{6., 56, 0.582203, 0.83586},
{6., 57, 0.583993, 0.838513},
{6., 58, 0.585727, 0.841082},
{6., 59, 0.587406, 0.843571},
{6., 60, 0.589035, 0.845982}}


Where I have $\beta_1(\lambda_1,\lambda_2)$ and $\beta_2(\lambda_1,\lambda_2)$, for {$\lambda_1$, 1.1, 6.0, 0.1} $\times$ {$\lambda_2$, 11, 60, 1}. The complete data set is in this file.

Now, I'd like to plot the series of $\beta_1$ for some set of $\lambda_2$ (i.e. Mod[$\lambda_2$,6] == 0]) as a function of $\lambda_2$ (x-axis). I tried to do some transform using Table[] and 2 iterators, but this was not very piratical, and I'll soon have more data. Also, I really believe Mathematica must have a solution for this, it's trivial.

In time, $\beta_1$ and $\beta_2$ are loss rates, so, if anybody would also have nice suggestions for plots, I'd be very glad. This is the one I could make so far, but it's too polluted - I'm a complete beginner at Mathematica: $\beta_1 \to$ Full line $\beta_2 \to$ Dashed line

Please, I'm getting desperate. All I want is to run a simple query to use this table to create a plot, that splits the data in series of $\lambda_2$, so I can have $\beta_1^{\lambda_2}(\lambda_1)$. I really need this ASAP, and I'm starting to considering take this data to another program, that would do this in 3 minutes.

Thank you all for your help!

Regards!

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Jun 25 '15 at 13:04
• Hey, thank you for the warming welcome. I read the tutorial, it's practical. However, I have 2 (meta-)questions: how can I tell SE to recognize Mathematica input? And would you have any suggestions to make my question better? – Guilherme Thompson Jun 25 '15 at 13:25
• Ordinarily, when a figure is inserted using the picture icon above the edit window, it just appears. Yours did not for some reason, so I fixed it. You can find general discussions of how to format questions and answers in Mathematica meta. I suggest you clarify how the β are defined. – bbgodfrey Jun 25 '15 at 13:38
• Did my post help you to understand how you can construct queries on your data ? Please give quick feedback if you need quick help. – SquareOne Jun 25 '15 at 19:25
• Yes, I just tried it, and it works nicely for the queries. However, I could not do exactly what I meant, basically, I'd like to have a plot for $\beta_1^{\lambda_2}(\lambda_1)$. I think the data must look like this: {{$\lambda_2$, {$\lambda_1$,$\beta_1$},{$\lambda_1$,$\beta_1$},{$\lambda_1$,$\beta_1$},(...)},{$\lambda_2$, {$\lambda_1$,$\beta_1$},{$\lambda_1$,$\beta_1$},{$\lambda_1$,$\beta_1$},(...)},{$\lambda_2$, {$\lambda_1$,$\beta_1$},{$\lambda_1$,$\beta_1$},{$\lambda_1$,$\beta_1$},(...)},(...)} – Guilherme Thompson Jun 25 '15 at 19:28

### 1.

This is a quick start and concerns your request : b1[lam2] such that Mod[lam2,6]==0

mydata = Import["/yourPathTo/data.txt", "Table"]

myXY = Cases[mydata, {lam1_, lam2_, b1_, b2_} /; Mod[lam2, 6] == 0 :> {lam2, b1}]

ListPlot[myXY] ### 2. Edit

Here is a way to plot your beta1[lambda1] grouped by identical lambda2.

Here are all the lambda2 values:

allLam2 = mydata[[All, 2]] // Union


{11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60}

Then

Cases[mydata, {lam1_, lam2_, b1_, b2_} /; lam2 == # :> {lam1, b1}] & /@ allLam2 //
ListPlot[Thread[Tooltip[#, allLam2]], Joined -> True, PlotLegends -> allLam2] & • FYI: You've got my vote. My answer is intended to be complementary. For example the lam2 fields are all integers which makes GroupBy easy, but in cases with Real data == will be necessary. – Mr.Wizard Jun 26 '15 at 8:42
• @Mr.Wizard Complementary approaches are the purpose of this forum ! ;) I was actually surprised to be the only one to answer for such a general question which may be of interest for many new Mma users ... I understand there are tons of similar questions ( like "How I import, select and plot my data"). Is there (or should it be) a reference post for this ? (Maybe i should post this to Meta ?) – SquareOne Jun 26 '15 at 9:14
• I'll be frank: I am sleeping worse than usual recently and it makes it harder to think clearly. At first glance I did not understand the question so I passed over it. After seeing your answer I thought "oh, so that's what he meant." I know there are other questions that are related but again the thinking is not so good and I can't remember. – Mr.Wizard Jun 26 '15 at 9:17

SquareOne's second example is inefficient as the data must be scanned repeatedly, once for each value in allLam2. A more efficient approach is to use GatherBy or GroupBy, or occasionally a Sow/Reap pairing.

mydata = Import["data.txt", "Table"];

selection = GroupBy[mydata, Extract -> Extract[{{1}, {3}}]];

ListLinePlot[Values @ selection, PlotLegends -> Keys @ selection] • I agree but ... Actually, I thought and tried first with GatherBy and GroupBy but I was not happy with it as it seemed less friendly and suitable for the OP profile (new Mma user) than with Cases with the explicit pattern matching. It is clear it is a perfect job for GroupBy ... i had some complementary approach in mind ... – SquareOne Jun 26 '15 at 9:00