Skip to content
Prev 301095 / 398502 Next

Regular Expression

If they are all formatted as your example, substr() would be simpler:

MONTH <- c("2012-07", "2001-07", "2002-01")
QUARTER <- c("2012-3", "2001-3", "2002-1")
YEAR <- c(2013, 2001, 2002)
Inp <- data.frame(MONTH, QUARTER, YEAR)
Out <- data.frame(MONTH=substr(MONTH, 6, 8),
     QUARTER=substr(QUARTER, 6, 7), YEAR)

This assumes MONTH and QUARTER are character strings and not dates.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352