Find sub-sequences of identical elements in a vector.

subSeq(x, item = NULL)

sub_seq(x, item = NULL)

is_grouped(x)

rle2(x)

Arguments

x

An atomic vector or a factor.

item

Optionally a specific value to look for in x.

Value

A dataframe.

Details

  • sub_seq is synonymous with subSeq

  • rle2 is identical to rle (from base) but rle2 works on factors as input (a factor is coerced to character).

  • is_grouped checks if the values in x are clustered into the smallest number of clusters.

See also

Author

Søren Højsgaard, sorenh@math.aau.dk

Examples


x <- c(1, 1, 1, 0, 0, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2, 3)
(ans <- subSeq(x))
#>   first last slength midpoint value
#> 1     1    3       3        2     1
#> 2     4    5       2        5     0
#> 3     6    8       3        7     1
#> 4     9   11       3       10     2
#> 5    12   12       1       12     1
#> 6    13   15       3       14     2
#> 7    16   16       1       16     3
ans$value
#> [1] 1 0 1 2 1 2 3

## Notice: Same results below
subSeq(x, item=1)
#>   first last slength midpoint value
#> 1     1    3       3        2     1
#> 2     6    8       3        7     1
#> 3    12   12       1       12     1
subSeq(x, item="1")
#>   first last slength midpoint value
#> 1     1    3       3        2     1
#> 2     6    8       3        7     1
#> 3    12   12       1       12     1

xc <- as.character(x)
(ans<-subSeq(xc))
#>   first last slength midpoint value
#> 1     1    3       3        2     1
#> 2     4    5       2        5     0
#> 3     6    8       3        7     1
#> 4     9   11       3       10     2
#> 5    12   12       1       12     1
#> 6    13   15       3       14     2
#> 7    16   16       1       16     3
ans$value
#> [1] "1" "0" "1" "2" "1" "2" "3"

## Notice: Same results below
subSeq(xc, item="1")
#>   first last slength midpoint value
#> 1     1    3       3        2     1
#> 2     6    8       3        7     1
#> 3    12   12       1       12     1
subSeq(xc, item=1)
#>   first last slength midpoint value
#> 1     1    3       3        2     1
#> 2     6    8       3        7     1
#> 3    12   12       1       12     1

is_grouped(x)
#> [1] FALSE
is_grouped(sort(x))
#> [1] TRUE
is_grouped(xc)
#> [1] FALSE
is_grouped(sort(xc))
#> [1] TRUE