Q:

delete a row where the order is wrong within a group

I have a data with about 1000 groups each group is ordered from 1-100(can be any number within 100).
As I was looking through the data. I found that some groups had bad orders, i.e., it would order to 100 then suddenly a 24 would show up.
How can I delete all of these error data

As you can see from the picture above(before -> after), I would like to find all rows that don't follow the order within the group and just delete it.
Any help would be great!

A:

lag will compute the difference between the current value and the previous value, diff will be used to select only positive difference i.e. the current value is greater than the previous value. min is used as lag give the first value NA. I keep the helper column diff to check, but you can deselect  using %>% select(-diff)
 library(dplyr)
 df1 %>% group_by(gruop) %>% mutate(diff = order-lag(order)) %>% 
         filter(diff >= 0 | order==min(order))

   # A tibble: 8 x 3
   # Groups:   gruop [2]
   gruop order diff
    <int> <int> <int>
 1     1     1    NA
 2     1     3     2
 3     1     5     2
 4     1    10     5
 5     2     1    NA
 6     2     4     3
 7     2     4     0
 8     2     8     4

Data
df1 <- read.table(text="
              gruop order
              1 1
              1 3
              1 5
              1 10
              1 2
              2 1
              2 4
              2 4
              2 8
              2 3
               ",header=T, stringsAsFactors = F)

