title: R: Compare across IDs within the same data frame
body: <p>I have the following dataset:</p>
  
  <pre><code>df &lt;- data.frame(c(1,1,1,2,2,2,2,3,3,3,3,4,4,4,5,5,5), c("a","a","a","b","b","b","b","b","b","b","b",
                                                           "a","a","a","b","b","b"),
                   c(300,295,295,25,25,25,25,25,20,20,20,300,295,295,300, 295,295), 
                   c("c","d","e","f","g","h","i","j","l","m","n","o","p","q","r","s","t"))
  colnames(df) &lt;- c("ID", "Group", "Price", "OtherNumber")
  
  &gt; df
     ID Group Price OtherNumber
  1   1     a   300           c
  2   1     a   295           d
  3   1     a   295           e
  4   2     b    25           f
  5   2     b    25           g
  6   2     b    25           h
  7   2     b    25           i
  8   3     b    25           j
  9   3     b    20           l
  10  3     b    20           m
  11  3     b    20           n
  12  4     a   300           o
  13  4     a   295           p
  14  4     a   295           q
  15  5     b   300           r
  16  5     b   295           s
  17  5     b   295           t
  </code></pre>
  
  <p>I want to compare the first price of subsequent IDs. Only if the two subsequent IDs have the same initial price and are in the same group, I want to flag them. Just in case this was not very clear, here an example: I compare the first and second ID, but both the group (a vs. b) and the initial price is a mismatch (300 vs. 25). On the other hand, between ID 2 and 3, they are both in group b and have the same initial price of 25 (cf. row 4 and 8). The prices afterwards do not really matter as they may differ. </p>
  
  <p>I figure, I must be able to work with the dplyr package and have determined a very rough solution (which does not yet work).</p>
  
  <pre><code># Load dplyr
  library(dplyr)
  
  # Assign row numbers within IDs
  df1 &lt;- df %&gt;%
    group_by(ID) %&gt;%
    mutate(subID = row_number())
  
  # Isolate first observation in ID 
  df2 &lt;- df1[df1$subID == 1,]
  
  # Set up loop to iterate through IDs
  for (i in 2:length(df2)) {
    if (df2$Price[i] - df2$Price[i - 1] == 0) {
      df2$flag &lt;- TRUE
    } else {
      df2$flag &lt;- FALSE
    }
  }
  
  </code></pre>
  
  <p>If you tell me that this is the only possible solution, I will obviously devote more resources to it, but I am sure there must be an easier solution. I checked on SO and maybe I missed something, but I was not able to find anything going into this direction. Thanks!</p>
  
  <p>The output I want to get looks something like this: </p>
  
  <pre><code>   ID Group Price OtherNumber   flag
  1   1     a   300           c  FALSE
  2   1     a   295           d  FALSE
  3   1     a   295           e  FALSE
  4   2     b    25           f   TRUE
  5   2     b    25           g   TRUE
  6   2     b    25           h   TRUE
  7   2     b    25           i   TRUE
  8   3     b    25           j   TRUE
  9   3     b    20           l   TRUE
  10  3     b    20           m   TRUE
  11  3     b    20           n   TRUE
  12  4     a   300           o  FALSE
  13  4     a   295           p  FALSE
  14  4     a   295           q  FALSE
  15  5     b   300           r  FALSE
  16  5     b   295           s  FALSE
  17  5     b   295           t  FALSE
  </code></pre>