title: Which variables in dataset are constant within id
body: <p>I've got a data set with a bunch of near-duplicate observations for each individual. I'm trying to determine exactly which variables are changing across rows for each specific ID.</p>
  
  <p>I started by inspection:</p>
  
  <pre><code>dt[ , count := .N, by = id][count &gt; 1, ]
  </code></pre>
  
  <p>(also using <code>head</code> and <code>tail</code>), but there are 27000 (total) repeated observations--there's no way by inspection to be sure which variables are changing.</p>
  
  <p>Is there anything better than the brute force approach?
  In particular I'm hoping to avoid a loop but I can't see how...</p>
  
  <pre><code>dt[ , count := .N, by = id]
  for (var in setdiff(names(dt), c("id", "count"))){
    if (nrow(dt[ , list(.N, count), by = c("id", var)][N &lt; count, ]) &gt; 0){
      print(var)
    }
  }
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>Here's an example data set to clarify:</p>
  
  <pre><code>set.seed(2304)
  DT &lt;- data.table(a = rep(sample(5), each = 3),
                   b = sample(15),
                   c = rep(LETTERS[1:5], each = 3),
                   d = rnorm(15),
                   e = rep(6:10, each = 3),
                   f = 15:1,
                   grp = paste0("Group ", rep(1:5, each = 3)))
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>c("a", "c", "e")
  </code></pre>
  
  <p>because those variables don't change for <em>any</em> fixed value of <code>grp</code>.</p>