title: understanding apply and outer function in R
body: <p>Suppose i have a data which looks like this</p>
  
  <pre><code>ID A B C 
  1  X 1 10
  1  X 2 10
  1  Z 3 15
  1  Y 4 12
  2  Y 1 15
  2  X 2 13
  2  X 3 13
  2  Y 4 13
  3  Y 1 16
  3  Y 2 18
  3  Y 3 19
  3  Y 4 10
  </code></pre>
  
  <p>I Wanted to compare these values with each other so if an ID has changed its value of A variable over a period of B variable(which is from 1 to 4) it goes into data frame K and if it hasn't then it goes to data frame L. </p>
  
  <p>so in this data set K will look like </p>
  
  <pre><code>ID A B C 
  1  X 1 10
  1  X 2 10
  1  Z 3 15
  1  Y 4 12
  2  Y 1 15
  2  X 2 13
  2  X 3 13
  2  Y 4 13
  </code></pre>
  
  <p>and L will look like </p>
  
  <pre><code>ID A B C 
  3  Y 1 16
  3  Y 2 18
  3  Y 3 19
  3  Y 4 10
  </code></pre>
  
  <p>In terms of nested loops and if then else statement it can be solved like following </p>
  
  <pre><code>for ( i in 1:length(ID)){
  m=0
  for (j in 1: length(B)){
  ifelse( A[j] == A[j+1],m,m=m+1)
  }
  ifelse(m=0,  L=c[,df[i]], K=c[,df[i]])
  }
  </code></pre>
  
  <p>I have read in some posts that in R nested loops can be replaced by <code>apply</code> and <code>outer</code> function. if someone can help me understand how it can be used in such circumstances. </p>