title: R switch in function
body: <p>I am trying to build a switch into an R function. The function should perform <code>gsub</code> commands on a data frame, depending on the dimension of the input.</p>
  
  <p>Here is some example data:</p>
  
  <pre><code>X1 &lt;- c("NUERNBERG","BERLIN","WERNE")
  X2 &lt;- c("REGENSBURGERSTR","WARSCHAUERSTRASSE","WIEHAGEN")
  X3 &lt;- c("89","23","1")
  x &lt;- cbind(X1,X2,X3)
  </code></pre>
  
  <p>and my function:</p>
  
  <pre><code>function &lt;- function(x, hnrstr){
    switch(hnrstr,
           "1" = {
             x &lt;- data.frame(X1=x[1],X2=x[2],X3=x[3])
             y &lt;- x 
             y[ ,"X2","X3"] &lt;- gsub(" {2, }"," ",y[ ,"STRASSE","HAUSNUMMER"])  
             x &lt;- y
           },
           "2" = {
             x &lt;- data.frame(X1=x[1],X2=x[2])
             P &lt;- x 
             P[ ,"X2"] &lt;- gsub(" {2, }"," ",y[ ,"X2"])
             x &lt;- P 
           }
    )
    return(x)
  }
  
  apply(x,1,function, hnrstr="1")
  </code></pre>
  
  <p>which gives the error:</p>
  
  <pre><code>Error in drop &amp;&amp; !has.j : invalid 'x' type in 'x &amp;&amp; y'
  </code></pre>