# Day 21 – little helper get_sequence

**r-bloggers – STATWORX**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

We at STATWORX work a lot with R and we often use the same little helper functions within our projects. These functions ease our daily work life by reducing repetitive code parts or by creating overviews of our projects. At first, there was no plan to make a package, but soon I realised, that it will be much easier to share and improve those functions, if they are within a package. Up till the 24th December I will present one function each day from `helfRlein`

. So, on the 21th day of Christmas my true love gave to me…

## What can it do?

This little helper returns indices of recurring patterns. It works with numbers as well as with characters. All it needs is a vector with the data, a pattern to look for and a minimum number of occurrences.

## How to use it?

Let's create some time series data with the following code.

library(data.table) # random seed set.seed(20181221) # number of observations n <- 100 # simulationg the data ts_data <- data.table(DAY = 1:n, CHANGE = sample(c(-1, 0, 1), n, replace = TRUE)) ts_data[, VALUE := cumsum(CHANGE)]

This is nothing more than a random walk, since we sample between going down (`-1`

), going up (`1`

) or staying at the same level (`0`

). Our time series data looks like this:

Assume we want to know the date ranges when there was no change for at least four days in a row.

ts_data[, get_sequence(x = CHANGE, pattern = 0, minsize = 4)] min max [1,] 45 48 [2,] 65 69

We can also answer the question, if the pattern "down-up-down-up" is repeating anywhere:

ts_data[, get_sequence(x = CHANGE, pattern = c(-1,1), minsize = 2)] min max [1,] 88 91

With these two inputs, we can update our plot a little bit by adding some `geom_rect`

!

### Code for the plot

rect <- data.table( rbind(ts_data[, get_sequence(x = CHANGE, pattern = c(0), minsize = 4)], ts_data[, get_sequence(x = CHANGE, pattern = c(-1,1), minsize = 2)]), GROUP = c("no change","no change","down-up")) ggplot(ts_data, aes(x = DAY, y = VALUE)) + geom_line() + geom_rect(data = rect, inherit.aes = FALSE, aes(xmin = min - 1, xmax = max, ymin = -Inf, ymax = Inf, group = GROUP, fill = GROUP), color = "transparent", alpha = 0.5) + scale_fill_manual(values = statworx_palette(number = 2, basecolors = c(2,5))) + theme_minimal()

## Overview

To see all the other functions you can either check out our GitHub or you can read about them here.

Have a merry advent season!

STATWORX

is a consulting company for data science, statistics, machine learning and artificial intelligence located in Frankfurt, Zurich and Vienna. Sign up for our NEWSLETTER and receive reads and treats from the world of data science and AI.

Sign Up Now!

Der Beitrag Day 21 – little helper get_sequence erschien zuerst auf STATWORX.

**leave a comment**for the author, please follow the link and comment on their blog:

**r-bloggers – STATWORX**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.