title: Using loop and subset to create different data.frames
body: <p>I have a data.frame in R. I want to create <strong>three</strong> different data.frame using a <strong>loop</strong> and <strong>subset</strong> function. I have a large data.frame, but what I need is in the example below:</p>
  
  <pre><code>sex&lt;-c("M","M","M","F","M","F")
  age&lt;-c(20,18,17,20,18,17)
  name&lt;-c("John", "Joseph", "Bill", "Sarah", "Robert", "Dana")
  data&lt;-data.frame(sex, age, name)
  
  &gt;data
          sex  age   name
      1   M    20    John
      2   M    18    Joseph
      3   M    17    Bill
      4   F    20    Sarah
      5   M    18    Robert
      6   F    17    Dana
  </code></pre>
  
  <p>What I want is:</p>
  
  <pre><code>&gt;age17
        sex   age   name
      1 M     17    Bill
      2 F     17    Dana
  
  &gt;age18
        sex   age   name
      1 M     18    Joseph
      2 M     18    Robert
  
  
  &gt;age20
        sex   age   name
      1 M     20    John
      2 F     20    Sarah
  </code></pre>
  
  <p>I can use the commands below: </p>
  
  <pre><code>&gt;age17&lt;-subset(data, data[,2]==17)
  &gt;age18&lt;-subset(data, data[,2]==18)
  &gt;age20&lt;-subset(data, data[,2]==20)
  </code></pre>
  
  <p><strong>But it is possible to use a loop to reduce the size of commands?</strong></p>