title: checking every two adjacent elements in rows of a matrix
body: <p>I'm new to coding in R, perhaps my question maybe too easy.</p>
  
  <p>I have a matrix "data" of dimension 299*5992. Each element is either A1 or A2 
  so I want to concentrate information of the matrix like this:</p>
  
  <pre><code>XD=matrix(,dim(data)[1],(dim(data)[2])/2 )
  for ( i in dim(XD)[1])
  {
    for (j in dim(XD)[2])
    {
      if (data[i,2*j]==data[i,2*j-1])
      {XD[i,j]=-1}
      else
      {XD[i,j]=1
      }
    }
  }  
  </code></pre>
  
  <p>So I have 2 questions:</p>
  
  <ol>
  <li>How to do this more efficiently?</li>
  <li>When I call an element like XD[1,1] I get NA  even though I'm
  sure that matrix data is loaded correctly.</li>
  </ol>