0
library(zoo)
library(lubridate)
yearmon <- as.yearmon(c("01-10", "02-15", "03-30"), "%m-%y")
for (i in yearmon) {
if (year(yearmon[i]) > 2020) {
year(yearmon[i]) <- year(yearmon[i]) - 100
}}

Error in if (year(yearmona[i]) > 2020) { : 
missing value where TRUE/FALSE needed

The idea is to take data with incorrect years > 2020, and put them back to 19XX form.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
Albatrosspro
  • 127
  • 6

2 Answers2

1

Here, we can use an ifelse and also the assignment part won't work

as.yearmon(paste(format(yearmon, "%b"), 
     ifelse(year(yearmon) > 2020, year(yearmon)-100, year(yearmon))))
#[1] "Jan 2010" "Feb 2015" "Mar 1930"
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Zoo's yearmon objects are year + fractional year, so you subtract 100 from anything over 2020:

> yearmon <- as.yearmon(c("01-10", "02-15", "03-30"), "%m-%y")
> yearmon
[1] "Jan 2010" "Feb 2015" "Mar 2030"
> yearmon[yearmon > 2020] = yearmon[yearmon > 2020] - 100
> yearmon
[1] "Jan 2010" "Feb 2015" "Mar 1930"

this doesn't require lubridate, or any format conversion etc.

Spacedman
  • 92,590
  • 12
  • 140
  • 224