Q:

Subset all data each of which have a following different number

Suppose a vector in the simplest case:
value = c(0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,1,1,1,1,1,0,0,0,0,0)

position = c(1:length(value))

data = cbind(value, position)

How can I directly subset those values marked in quotes shown below ?
value = c(0,0,0,'0',1,1,1,1,1,'1',2,2,2,2,2,2,'2',1,1,1,1,'1',0,0,0,0,0)

Certainly, as well as their position.

A:

value = c(0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,1,1,1,1,1,0,0,0,0,0)

position = c(1:length(value))

data = cbind(value, position)

library(dplyr)

data.frame(data) %>% filter(value != lead(value))

#   value position
# 1     0        4
# 2     1       10
# 3     2       17
# 4     1       22

The philosophy is to filter (i.e. keep) the rows where value is different the the value in the next row.

A:

rle will give you the lengths and the values of consecutive runs of same elements in a vector.
rle(value)
#Run Length Encoding
#  lengths: int [1:5] 4 6 7 5 5
#  values : num [1:5] 0 1 2 1 0

Then you can manipulate either the lengths or the values as per your need.
data[head(cumsum(rle(value)$lengths), -1),]
#     value position
#[1,]     0        4
#[2,]     1       10
#[3,]     2       17
#[4,]     1       22

