1
$\begingroup$

I want to calculate an average where points that fall under a specified area are omitted.

I have tried using DeleteCases and If, but have had no luck so far.

This is the code I have so far:

(*data*)
countries = CountryData["Countries"];
fem = (Map[CountryData[#, "FemaleLifeExpectancy"] &, countries] // 
     DeleteMissing // QuantityMagnitude);
male = (Map[CountryData[#, "MaleLifeExpectancy"] &, countries] // 
     DeleteMissing // QuantityMagnitude);
pts = Transpose[{fem, male}];

(*desired area*)

pol = Polygon[{{55, 60}, {60, 70}, {70, 55}, {60, 50}}];
rm = RegionMember[pol];

DeleteCases[rm[pts], True];
$\endgroup$

1 Answer 1

2
$\begingroup$
p1 = ListPlot[pts, Prolog -> {Lighter@Cyan, pol}];
pts2 = Pick[pts, Not /@ (RegionMember[pol] /@ pts)];
p2 = ListPlot[pts2, PlotStyle -> Red
  , Prolog -> {Lighter@Cyan, pol}];
GraphicsRow[{p1, p2}]

enter image description here

m = Mean[pts2];
ListPlot[pts2
 , Epilog -> {
   Red, AbsolutePointSize[6], Point@m
   }
 ]
$\endgroup$
2
  • $\begingroup$ What would be a way to calculate the average for females and males separately? $\endgroup$ Commented Feb 5, 2022 at 12:55
  • $\begingroup$ First@m and Last@m ? or explicitly as Mean[pts2[[All, 1]]] etc. $\endgroup$
    – Syed
    Commented Feb 5, 2022 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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