symbolrush
symbolrush

Reputation: 7457

Parse string, extract two-digit year and complete into four digit format

I have strings like

y1 <- "AB99"
y2 <- "04CD"
y3 <- "X90Z"
y4 <- "EF09"
y5 <- "12GH"

where I need to extract the two digit year and complete it into a four digit format. The input range is from 1990 - 2020.

The output should be:

"1999"
"2004"
"1990"
"2009"
"2012"

I tried:

fun <- function(x) {
  year <- readr::parse_number(x)
  if(year < 50) year <- paste0("20", year) else year <- paste0("19", year)
  return(year)
}

This works fine, except for the years 2000 - 2009 (testcase y2 and y4).

Which functions can help me to also work fine on those years?

Upvotes: 0

Views: 202

Answers (3)

s_baldur
s_baldur

Reputation: 33488

Using some basic regex, you can remove everything that is not a number and apply an ifelse() to prefix 19 or 20 as appropriate:

# Example data
y <- c(
  y1 = "AB99",
  y2 = "04CD",
  y3 = "X90Z",
  y4 = "EF09",
  y5 = "12GH"
)

# Extract only the number
num <- gsub("\\D", "", y) 
paste0(ifelse(num >= "90", "19", "20"), num)
# [1] "1999" "2004" "1990" "2009" "2012"

Alternatively, working with integers:

num <- as.integer(gsub("\\D", "", y)) # or as.integer(readr::parse_number(y))
num + ifelse(num >= 90L, 1900L, 2000L)
# [1] 1999 2004 1990 2009 2012

Upvotes: 2

rj-nirbhay
rj-nirbhay

Reputation: 679

The parse_number will return 4, single digit number of y2 case. To get desired output you can add one more condition on number of characters as given below:

fun_1 <- function(x) {
  year <- readr::parse_number(x)
  #cat("year  is ",year,"\n") #added for check
  if(year < 50 & nchar(year)<2){
    year <- paste0("20","0", year) 

  } else {
    year <- paste0("19", year)
    }
 # cat("Year post changes",year,"\n") # added for check,  
  print(year)
}

output:

fun_1(y2)
year  is  4 
Year post changes 2004 

I have added the cat step just for checks.

Upvotes: 0

Martin Gal
Martin Gal

Reputation: 16978

A number doesn't have a leading 0, therefore you don't get your desired output. Using stringr and the str_pad function should solve your issue.

fun <- function(x) {
  year <- readr::parse_number(x)
  if (year < 50) {
     year <- paste0("20", stringr::str_pad(year, 2, side="left", "0")) 
  } else {
     year <- paste0("19", year)
  }
  return(year)
}

Another hint: use return instead of print.

Upvotes: 1

Related Questions