1

I'm a new user of Mathematica and now I am confused by a problem.

Now I have about 2 million rows of data and 3 columns, i.e. a 2million*3 table. What I am trying to do is select rows of data satisfying two 'AND' conditions, the first column should equal to 18 to 60 AND the second column equal to 1 to 5(both conditions with step 1). Then I have the data in the 3rd column with the same 1st and 2nd column value. The next step I will calculate the Mean or Median or apply distribution analysis base on the Selected results. For example, I need select rows with conditions of Cloume1=18 AND Column2=1.

I wrote

Do[Select[mytable, If[#[[1]] == i&&#[[2]] == j, True, False] &], {i, 18, 60, 1},{j,1,5,1}]

and nothing(NULL) is shown in the output.

I put some sample data as shown below.

{{18, 1, 3.195}, {18, 1, 3.902}, {60, 1, 5.4333}, {60, 1, 3.1}, {60,1, 6.8594}, {60, 1, 3.666}, {60, 1, 3.902}, {60, 1, 1.2666}, {60, 1,3.902}, {60, 1, 2.3}, {60, 1, 4.3}, {60, 1, 3.902}, {60, 1,3.902}, {60, 1, 3.563}, {60, 1, 3.902}, {60, 1, 51.835}, {60, 1,3.902}}

How can I fix it? Thank you for your help!

Rick
  • 29
  • 3
  • "first column should equal to 18 to 60" means that values in that column should be in 18 to 60 interval, or 18 or 60? 2) Do does not return anthing, you need to use Table 3) but this approach is not ok anyway you can start with: Select[mytable, (#[[1]] == 18 || #[[1]] == 60) && (#[[2]] == 1 || #[[2]] == 5)&], or Cases[mytable, { 18|60, 1|5, ___}]
  • – Kuba Sep 19 '18 at 07:59
  • If your mytable is very large, the code will be very slow, as the Do loop will make Select go thourhg the data $60\times 5$ times. You should avoid procedurical loops. Maybe something like Tally is already does the sorting you need. – Johu Sep 19 '18 at 08:23
  • Is your data an array? I.e., do all columns contain integers? – LLlAMnYP Sep 19 '18 at 08:34
  • Thank you all for your response!This is an awesome place to learn! The problem was posed when I was analysing a excel file exported from the population of a huge city data base. I have solved the problem in a very clumsy way(which is part of answer from sunt05):Median[Select[data, #[[1 ;; 2]] == {18, 2} &][[All, 3]]], and repeat this command from 18 to 60.(it took me some time). – Rick Sep 27 '18 at 07:49