title: Calculate centroids of a wings where all the points used to calculate are in the boundary in R
body: <p>Recently I've been working in a project relating to detect landmark from an insect's  wing using R language. 
  The input data:</p>
  
  <pre><code>    X.1    X    Y                     IMAGE ID    Scale isBoundary isLandmark
  1     1  193 1053 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  2     2  580 1127 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  3     3 1173 1229 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  4     4 1619 1285 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  5     5 1697 1278 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  6     6 2106 1229 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  7     7 2301 1109 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  8     8 2378 1025 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  9     9 2417  924 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  10   10 2323  832 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  11   11 2168  720 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  12   12 1963  620 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  13   13 1775  558 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  14   14 1734  550 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  15   15 1400  527 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  16   16  618  775 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          1
  17   17  295  898 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  18   18   72 1012 F_ST03_07.21.11_LT_02.tif  4 0.001099          1          0
  19   19  510 1021 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  20   20  520  988 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  21   21  600  961 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  22   22  571  918 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  23   23  584  881 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  24   24  573  841 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  25   25  539  838 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  26   26  547  814 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  27   27  835  906 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          1
  28   28  876  941 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          1
  29   29  878  978 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          1
  30   30  784 1086 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          1
  31   31 1224 1041 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          1
  32   32 1241  845 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          1
  33   33 1419  575 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  34   34 1455  701 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  35   35 1529  935 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  36   36 1560  939 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  37   37 1496 1133 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          1
  38   38 1570 1111 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  39   39 1736 1201 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  40   40 1640 1238 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  41   41 1799 1217 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  42   42 1814 1207 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  43   43 2020 1021 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  44   44 1715 1017 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  45   45 1998 1051 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  46   46 2170  931 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  47   47 2041  726 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  48   48 1777  845 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  49   49 2180  836 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  50   50 2333  929 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  51   51 2278 1019 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  52   52 2190  765 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  53   53  215 1010 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  54   54  230  978 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  55   55 1558  566 F_ST03_07.21.11_LT_02.tif  4 0.001099          0          0
  56   56  273 1129 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  57   57  576 1203 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  58   58  954 1258 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  59   59 1181 1299 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  60   60 1539 1371 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  61   61 1895 1407 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  62   62 2157 1381 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  63   63 2258 1340 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  64   64 2411 1238 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  65   65 2485 1158 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  66   66 2515 1070 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  67   67 2427  992 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  68   68 2280  898 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  69   69 2190  859 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  70   70 2059  808 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  71   71 1846  736 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          1
  72   72 1652  710 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  73   73 1286  771 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  74   74  813  939 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  75   75  533 1025 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  76   76  786 1147 F_ST03_07.21.11_LT_03.tif  5 0.001099          1          0
  77   77  713 1109 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  78   78  792 1082 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  79   79  756 1041 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  80   80  784 1014 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  81   81  776  986 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  82   82  970 1219 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          1
  83   83  972 1201 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  84   84  921 1209 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  85   85  905 1166 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          0
  86   86 1116 1107 F_ST03_07.21.11_LT_03.tif  5 0.001099          0          1
  </code></pre>
  
  <p>The method I use to calculate centroids:</p>
  
  <pre><code>centroids &lt;- aggregate(.~ID, data=data, mean, byisBoundary= TRUE)
  centroid1 &lt;- centroids[,c("ID","X","Y")]
  centroid &lt;- rename(centroid1,c("X"="X-centroid","Y"="Y-centroid"))
  data3 &lt;- merge(data1,centroid, by="ID")
  </code></pre>
  
  <p>By that I can get the centroid of each wing following the ID. However, this results from all the points of each wing.</p>
  
  <pre><code>  ID        X         Y
  1  4 1378.491  941.1091
  2  5 1511.562 1100.6042
  3  6 1285.222  968.0926
  </code></pre>
  
  <p>How can I calculate the centroid by the average of sum(X) and sum(Y) on those points whose isBoundary = 1 only. </p>
  
  <p>Many thanks.</p>