Question: **My start on an example**:

```
yr_min <- 20181
yr_max <- 20195

as.numeric(paste0(rep(
  seq(as.numeric(substr(yr_min, 1, 4)), 
      as.numeric(substr(yr_max, 1, 4))), 
  each = 3),
  c(1, 3, 5)))

[1] 20181 20183 20185 20191 20193 20195

```

**What is wrong with the above code**? It will not generalize beyond situations where `yr_min` ends in a `1` and `yr_max` ends in a `5`.

For example:

```
yr_min <- 20183
yr_max <- 20193

as.numeric(paste0(rep(
  seq(as.numeric(substr(yr_min, 1, 4)), 
      as.numeric(substr(yr_max, 1, 4))), 
  each = 3),
  c(1, 3, 5)))

[1] 20181 20183 20185 20191 20193 20195

```

The desired output is 

```
[1] 20183 20185 20191 20193

```

Answer: 
1) Generate the entire sequence of years then `Filter()` out anything which isn't 1,3,5 modulo 10.

```
rem_135_mod10 <- function(x) { (x %% 10) %in% c(1,3,5) }

Filter(rem_135_mod10, seq(yr_min, yr_max))
# 20181 20183 20185 20191 20193 20195

```

* This is functional programming in R with the constructs [Filter(), Map(), Reduce()...](https://stat.ethz.ch/R-manual/R-devel/library/base/html/funprog.html))
* as the others say, for these specific remainders 1,3,5, you can exploit that you only need step 2: `seq(yr_min, yr_max, by=2)`

or 2) generate the sequence of base decades 20180, 20190... and add the offsets 1,3,5 yourself. Same difference.
I would recommend using bona fide dates in your calculation. This allows us to take advantage of base R's `seq` function:

```
x <- seq(as.Date("2018/3/1"), as.Date("2019/3/1"), by="month")
x[format(x, "%m") %in% c("01", "03", "05")]

[1] "2018-03-01" "2018-05-01" "2019-01-01" "2019-03-01"

```

If you really want the exact format you have, you can easily do that with another call to `format`:

```
y <- x[format(x, "%m") %in% c("01", "03", "05")]
format(y, "%Y%m")

[1] "201803" "201805" "201901" "201903"

```

Or, for your *exact* output:

```
sub("(?<=\\d{4}).", "", format(y, "%Y%m"), perl=TRUE)

[1] "20183" "20185" "20191" "20193"

```