1
$\begingroup$

I have data set in form {section, Arcangale ,X,Y, type}. Section is the number between 1 to 3 (1,2,3), Arcangale is number change from 0 to 1 something like this (0.0176575), x and y are coordinate (example:(1685.34, 1319.77)), type is number between 1 to 15. I want to separate/select this data set base on the section and type. for example first I want to get all of the data that section==1 and the select base on the type as example type==12. At end I want to have (Arcangale ,X,Y) coordinate for all of the data that have (for example) section==1 and type==12.

$\endgroup$
2
  • 2
    $\begingroup$ Take a look at Cases. For example: Cases[list, {1, _, _, _, 12}] or Cases[list, {1, aa_, x_, y_, 12} :> {x, y}]. $\endgroup$
    – Domen
    Jan 19, 2022 at 21:03
  • $\begingroup$ Maybe GroupBy (which returns associations) or GatherBy (which returns nested lists). $\endgroup$
    – MarcoB
    Jan 19, 2022 at 23:00

1 Answer 1

3
$\begingroup$

You may use Select and Query.

With example data

n = 200;
SeedRandom[987];
data =
  Transpose@
   {
    RandomInteger[{1, 3}, n]
    , RandomReal[1, n]
    , Sequence @@ 
     Partition[RandomInteger[{130000, 170000}, 2 n]/100., n]
    , RandomInteger[{1, 15}, n]
    };
Short@data
{{3,0.932128,1393.48,1348.83,5},<<198>>,{2,0.526802,1503.34,1660.94,1}}

Then you can Select data directly using index positions with Part.

Select[
  data,
  #[[{1, -1}]] == {1, 12} &
  ][[All, {3, 4}]]
{{1545.15, 1531.7}, {1668.49, 1364.58}, {1312.75, 1566.74}, 
 {1331., 1478.98}, {1448.64, 1390.46}, {1587.36, 1367.59}, 
 {1360.02, 1694.58}, {1570.55, 1480.11}, {1401.99, 1472.49}}

To use named columns instead of indices an Association can be constructed with AssociationThread.

colname = {"Section", "Arcangale", "X", "Y", "Type"};
assocList = AssociationThread[colname, #] & /@ data;
Short@assocList
{<|Section->3,Arcangale->0.932128,X->1393.48,Y->1348.83,Type->5|>,<<198>>,<|Section->2,<<3>>,Type->1|>}

Then

res1 = Query[
   Select[#Section == 1 && #Type == 12 &]
   , {"X", "Y"}
   ]@assocList
{<|X->1545.15,Y->1531.7|>,<|X->1668.49,Y->1364.58|>,<|X->1312.75,Y->1566.74|>, 
 <|X->1331.,Y->1478.98|>,<|X->1448.64,Y->1390.46|>,<|X->1587.36,Y->1367.59|>,
 <|X->1360.02,Y->1694.58|>,<|X->1570.55,Y->1480.11|>,<|X->1401.99,Y->1472.49|>}

Use Values for only the values.

Values@res1

List of associations are good but memory hungry for tabular data. If your data is large enough you can use ResourceFunction["TableSet"] for named column tabular data with less memory that list of associations.

tsdata = ResourceFunction["TableSet"][colname, data]

Mathematica graphics

Then

Query[
  Select[#Section == 1 && #Type == 12 &]
  , {#X, #Y} &
  ]@tsdata
{{1545.15, 1531.7}, {1668.49, 1364.58}, {1312.75, 1566.74}, 
 {1331., 1478.98}, {1448.64, 1390.46}, {1587.36, 1367.59}, 
 {1360.02, 1694.58}, {1570.55, 1480.11}, {1401.99, 1472.49}}

Notice about 4.5 times less memory needed than for list of associations.

ByteCount /@ {assocList, tsdata}
{168064, 36640}

Hope this helps.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.