title: Convert data frame to list
body: <p>I am trying to go from a data frame to a list structure in R (and I know technically a data frame is a list). I have a data frame containing reference chemicals and their mechanisms different targets. For example, estrogen is an estrogen receptor agonist. What I would like is to transform the data frame to a list, because I am tired of typing out something like:</p>
  
  <pre><code>refchem$chemical_id[refchem$target=="AR" &amp; refchem$mechanism=="Agonist"]
  </code></pre>
  
  <p>every time I need to access the list of specific reference chemicals. I would much rather access the chemicals by:</p>
  
  <pre><code>refchem$AR$Agonist
  </code></pre>
  
  <p>I am looking for a general answer, even though I have given a simplified example, because not all targets have all mechanisms.</p>
  
  <p>This is really easy to accomplish with a loop:</p>
  
  <pre><code>example &lt;- data.frame(target=rep(c("t1","t2","t3"),each=20),
                        mechan=rep(c("m1","m2"),each=10,3),
                        chems=paste0("chem",1:60))
  oneoption &lt;- list()
  for(target in unique(example$target)){
    oneoption[[target]] &lt;- list()
    for(mech in unique(example$mechan)){
      oneoption[[target]][[mech]] &lt;- as.character(example$chems[ example$target==target &amp; example$mechan==mech ])
    }
  }
  </code></pre>
  
  <p>I am just wondering if there is a more clever way to do it. I tried playing around with <code>lapply</code> and did not make any progress.</p>