title: R: Subset data using for-loop
body: <p>I have a big dataset with neighbourhoods, but I only want a subset of those neighbourhoods for which I have a vector of characters. 
  I thought about subsetting this using a for loop, see data examples and code examples below. It seems like my for loop is actually looping over all the neighbourhoods - however - in my new dataset only one neighbourhood appears, instead of the subset with all the neighbourhoods. Can anybody tell me what I'm doing wrong in the codes? </p>
  
  <p>I was inspired by all these answers: 
  <a href="https://stackoverflow.com/questions/39955402/how-to-subset-the-dataframe-byusing-for-loop-and-if-condition-in-r">How to subset the dataframe byusing for loop and if condition in r</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19882085/subset-data-frame-in-r-using-loop/19882147">subset data frame in R using loop</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18382711/r-loop-through-data-frame-extracting-subset-of-data-depending-on-date">R: loop through data frame extracting subset of data depending on date</a></p>
  
  <p>Best. And thanks. </p>
  
  <p><strong>EXAMPLE DATA</strong></p>
  
  <p>Big dataset:</p>
  
  <pre><code>&gt; head(CBS, n=5)
  # A tibble: 5 x 37
                    `Wijken en buurten` `Aantal inwoners` `||Mannen` `||Vrouwen`
                                  &lt;chr&gt;             &lt;dbl&gt;      &lt;dbl&gt;       &lt;dbl&gt;
  1                             Alkmaar            108373      53659       54714
  2                                Zuid             14315       6785        7525
  3                            Kooimeer              2040        930        1105
  4   Dillenburg en Stadhouderskwartier              1310        605         700
  5 Staatsliedenkwartier en Landstraten              2130       1015        1110
  </code></pre>
  
  <p>But I only need  the information from these neighbourhoods:</p>
  
  <pre><code>&gt; head(buurten_2, n=5)
       buurten_2          
  [1,] "Oud-Overdie"      
  [2,] "Overdie-West"     
  [3,] "Overdie-Oost"     
  [4,] "Oosterhout"       
  [5,] "De Hoef III en IV"
  </code></pre>
  
  <p>These correspond to variable names in the 'Wijken en Buurten' column of CBS. </p>
  
  <p>These are the loops I've tried: <em>(of some I knew it wouldn't be working but I was desperate...)</em></p>
  
  <pre><code>for (i in 1:nrow(buurten_2)){
    if (CBS$`Wijken en buurten`[i] == buurten_2[i])
      data &lt;- append(data, CBS[i,])
    print(buurten_2[i])
  }
  
  for (i in 1:length(buurten_2)){
    temp &lt;- CBS[CBS$`Wijken en buurten`==buurten_2[i],]
    print(buurten_2[i])
  }
  
  for (i in 1:length(buurten_2)){
    data &lt;- subset(CBS, CBS$`Wijken en buurten` == buurten_2[i])
    print(buurten_2[i])
  }
  
  for (buurten in 1:nrow(buurten_2)){
    CBS %&gt;% 
      filter(CBS$`Wijken en buurten`[i] == buurten_2[buurten])
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>But that gives me this</strong></p>
  
  <pre><code># A tibble: 1 x 37
    `Wijken en buurten` `Aantal inwoners` `||Mannen` `||Vrouwen` `|||Ongehuwd` `||Gehuwd`
                  &lt;chr&gt;             &lt;dbl&gt;      &lt;dbl&gt;       &lt;dbl&gt;         &lt;dbl&gt;      &lt;dbl&gt;
  1      Oudorp-Centrum              1930        935         995           730        940
  </code></pre>