title: Efficient R code for finding indices associated with unique values in vector
body: <p>Suppose I have vector <code>vec &lt;- c("D","B","B","C","C")</code>.</p>
  
  <p>My objective is to end up with a list of dimension <code>length(unique(vec))</code>, where each <code>i</code> of this list returns a vector of indices which denote the locations of <code>unique(vec)[i]</code> in <code>vec</code>.</p>
  
  <p>For example, this list for <code>vec</code> would return:</p>
  
  <pre><code>exampleList &lt;- list()
  exampleList[[1]] &lt;- c(1) #Since "D" is the first element
  exampleList[[2]] &lt;- c(2,3) #Since "B" is the 2nd/3rd element.
  exampleList[[3]] &lt;- c(4,5) #Since "C" is the 4th/5th element.
  </code></pre>
  
  <p>I tried the following approach but it's too slow. My example is large so I need faster code:</p>
  
  <pre><code>vec &lt;- c("D","B","B","C","C")
  uniques &lt;- unique(vec)
  exampleList &lt;- lapply(1:3,function(i) {
      which(vec==uniques[i])
  })
  exampleList
  </code></pre>