title: Using ppx and nndist to find nearest neighbour of each type
body: <p>For a dataset with three covariates and a treatment indicator I'm trying to find each individual's nearest neighbour. In particular, I want to find the nearest neighbour in each of the treatment groups.</p>
  
  <pre><code># Generate a treatment indicator factor
  treatment &lt;- factor(data_train[,"a"], levels = c("0", "1"), labels = c("Untreated", "Treated"))
  
  # Put the covariate data into 'points' format
  pointpattern &lt;- ppx(data = data.frame(data_train[, c("Z1", "Z2", "Z3")], "Treatment" = treatment), coord.type = c("s", "s", "s", "m"))
  
  # Find the nearest neighbour of each type
  dists &lt;- nndist(X = pointpattern, by = marks(pointpattern))
  </code></pre>
  
  <p>However the object 'dists' is only a vector, which appears to be the nearest neighbour with the treatment group completely ignored.</p>
  
  <p>I've spent nearly a full day trying to figure out what Im doing wrong - please help!</p>