title: R - Nested IF statements with grep
body: <p>I'm trying to tag some rows in my data based on a couple of criteria in my data frame.</p>
  
  <p>My data looks like:</p>
  
  <pre><code>X &lt;- structure(list(Website = c("www.something.at", "www.something.nl", "www.something.ch", "www.something.dk", "www.something.at"), 
                      Country = c("German", "Netherlands", "German", "Denmark", "Austria")), 
                      .Names = c("Website", "Country"), row.names = c(NA, 10L), class = "data.frame")
  </code></pre>
  
  <p>What I need to do is add a new column which tags the data in a new column based on certain criteria. So where the country equals German I need to look at the website URL and using the IF function tag it with a different country name. I.e. Austria or Switzerland.</p>
  
  <p>I have got to the below and I'm hoping I'm missing something really simple, but the code works fine for tagging Switzerland, but in all other cases everything gets tagged as Austria.</p>
  
  <pre><code>    for(i in 1:nrow(X)){
      if(length(grep("German", X$Country[i]))&gt;0)
  
      if(length(grep("\\.at$", X$Website[i]))&gt;0)
      X$Website_2[i] &lt;- "Austria"
  
      else
      if(length(grep("\\.ch$", X$Website[i]))&gt;0)
      X$Website_2[i] &lt;- "Switzerland"
  
      }
  </code></pre>
  
  <p>Any help much appreciated!</p>