title: one hot encoding function
body: <p>I need to create a function that takes two integer numbers <code>x</code> and <code>N</code>, where <code>N &gt; x</code> and returns a vector of dimension <code>N</code> with all zeros with the exception of component <code>x</code>, in which it has a <code>1</code>.</p>
  
  <p>I managed to do it in the following way,</p>
  
  <pre><code>Function=function(x,N){
    vec=rep(0,N)
    r=as.integer(x)
    vec[r]=1
    return(vec)
  }
  </code></pre>
  
  <p>but it is incredibly slow when I need to iterate the process and apply it to a large number of realizations. On the other hand, a friend of mine is able to do the same thing with a single function of python (I think "OneHotEncoder") and it's super fast.</p>
  
  <p>I was wondering if there are functions in R that are suited for this purpose.  </p>