killonstar - 4 months ago 17

R Question

I have a lot of vectors, which looks something like this:

`a <- c(0,0,0,1,1)`

b <- c(1,0,0,0,1)

c <- c(0,0,1,1,1)

In all of these vectors have the values that are repeated three times in succession.

I need to somehow identify these repetitions. The main condition is that the value of repeated one after the other.

Duplicated() will not help, at least in the base.

The definition of such vectors is necessary in order then to remove them.

A suitable vector for my work.

`d <- c(1,0,1,0,0)`

Improper vector.

`e <- c(1,1,1,0,0)`

Answer

You might want to take a look at the `rle`

from the base package or the `rleid`

function from `data.table`

.

```
rle(c(0,0,0,1,1))
Run Length Encoding
lengths: int [1:2] 3 2
values : num [1:2] 0 1
library(data.table)
rleid(c(0,0,0,1,1))
[1] 1 1 1 2 2
```

Both will look at runs of the same number. The `rle`

function returns a list of lengths and values, and the `rleid`

function returns a vector counting up each time the number in the series changes.