title: Simple way of creating dummy variable in R
body: <p>I want to know how simply a dummy variables can be created. I found many similar questions on the dummy but either they are based on some external packages or technical.</p>
  
  <p>I have data like this : </p>
  
  <pre><code>df &lt;- data.frame(X=rnorm(10,0,1), Y=rnorm(10,0,1))
  df$Z &lt;- c(NA, diff(df$X)*diff(df$Y))
  </code></pre>
  
  <p>Z create a new variable within df ie product of change in X and change in Y. 
  Now I want to create a dummy variable D in df such that if : Z &lt; 0 then D==1, if Z >0 then D==0. </p>
  
  <p>I tried in this way : </p>
  
  <pre><code>df$D &lt;- NA
  for(i in 2:10) {
  if(df$Z[i] &lt;0 ) {
  D[i] ==1
  }
  if(df$Z[i] &gt;0 ) {
  D[i] ==0
  }}
  </code></pre>
  
  <p>This is not working. 
  I want to know why above code is not working (with easy way of doing this) and how dummy variables can be creating in R without using any external packages with little bit of explanation. </p>