As rle
says, you can use rle
.
rle(dat$WDIR) Run Length Encoding lengths: int [1:9] 1 1 4 1 1 3 1 1 1 values : int [1:9] 22 23 126 25 26 132 30 132 35
rle
returns an object with two components, lengths and values. We can use a piece of length to build a new column that identifies which values ββare repeated more than three times.
tmp <- rle(dat$WDIR) rep(tmp$lengths >= 3,times = tmp$lengths) [1] FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
This will be our new column.
newCol <- rep(tmp$lengths > 1,times = tmp$lengths) cbind(dat,newCol) DATETIME WDIR newCol 1 40360.04 22 FALSE 2 40360.08 23 FALSE 3 40360.12 126 TRUE 4 40360.17 126 TRUE 5 40360.21 126 TRUE 6 40360.25 126 TRUE 7 40360.29 25 FALSE 8 40360.33 26 FALSE 9 40360.38 132 TRUE 10 40360.42 132 TRUE 11 40360.46 132 TRUE 12 40360.50 30 FALSE 13 40360.54 132 FALSE 14 40360.58 35 FALSE
source share