title: Dataframes in nested for loop. <0 rows> (or 0-length row.names) error
body: <p>This is a subset of the data frame I am working on:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>ID</th>
  <th>FRUIT1</th>
  <th>FRUIT2</th>
  <th>FRUIT3</th>
  <th>VEG1</th>
  <th>VEG2</th>
  <th>VEG3</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  </tr>
  <tr>
  <td>3</td>
  <td>2</td>
  <td>1</td>
  <td>2</td>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  </tr>
  <tr>
  <td>4</td>
  <td>2</td>
  <td>2</td>
  <td>2</td>
  <td>1</td>
  <td>2</td>
  <td>1</td>
  </tr>
  <tr>
  <td>5</td>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>2</td>
  <td>1</td>
  <td>2</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>It consists of 5 subjects, for whom there is information on 3 fruits and 3 vegetables:</p>
  <ul>
  <li>1 = the subject does not eat the fruit/vegetable</li>
  <li>2 = the subject eats the fruit/vegetable</li>
  </ul>
  <p>I am interested in counting how many individuals eat the 9 possible combinations of fruits and vegetables (FRUIT1 with VEG1, FRUIT1 with VEG2, …).
  This is the script I wrote:</p>
  <pre><code># Read data 
  dataframe &lt;- read.csv(&quot;myfile.csv&quot;, header=TRUE)
  
  # Define variables
  FRUIT= names(dataframe)[2:4])
  VEG= names(dataframe[5:7]))
  
  # Check frequency of interactions
  for (fruit in FRUIT) {
      for (veg in VEG) {
          #Double-positive: keep only subjects that each both the fruit and the vegetable
          PP &lt;- dataframe[dataframe$fruit=='2' &amp; dataframe$veg=='2',]
          #Double-negative: keep only subjects that don’t eat any 
          AA &lt;- dataframe[dataframe$fruit=='1' &amp; dataframe$veg=='1',]
          #Only FRUIT-positive: keep only subjects that eat the fruit, but not the vegetable
          PA &lt;- dataframe[dataframe$fruit=='2' &amp; dataframe$veg=='1',]
          #Only VEG-positive: keep only the subject that eat the vegetable, but not the fruit
          AP &lt;- dataframe[dataframe$fruit=='1' &amp; dataframe$veg=='2',]
          # Print the name of the fruit, the vegetable, and the counts of each of the 4 categories 
      toprint &lt;- c(kir,hla,nrow(PP),nrow(AP),nrow(PA),nrow(AA))
      setwd(“~/Directory/“)
      write(toprint, file = &quot;NumberIndividuals.csv&quot;,ncolumns=6,append = TRUE, sep = &quot; &quot;)
      }
  }
  </code></pre>
  <p><strong>Problem</strong>: the script above works outside the for loops but in this nested for loops I get the following message: <code>&lt;0 rows&gt; (or 0-length row.names)</code> for PP, AA, PA and AP.  Why are the sub-datasets (PP, AA, PA and AP) empty in this case?</p>