Sumit Sumit - 1 month ago 5
R Question

How to extract year and date out of a string in R

I have a list of character with entries such as "31DEC2005". I want to extract the year as integer 2005, as well as date month "31DEC" out of it. Please suggest a method. Thanks.

Answer

Here is one alternative to extract the year as integer:

x <- c("31DEC2005", "28FEB2012", "16JUL2002", "01JAN2007")
sub("[[:digit:]]+[[:alpha:]]+","", x)
#[1] "2005" "2012" "2002" "2007"

Date-month is easiest to extract from @bartektartanus's solution using date functions in data.table package for instance:

y <- as.Date(x, "%d%b%Y")
library(data.table)
month(y)
#[1] 12  2  7  1
mday(y)
#[1] 31 28 16  1
paste(mday(y), month(y), sep = "-")
#[1] "31-12" "28-2"  "16-7"  "1-1"  

This solution would also work for year:

year(y)
#[1] 2005 2012 2002 2007
Comments