Skip to content
Prev 301849 / 398506 Next

year extraction over a list

Hello,

Try the following.


x <- c("text, text, 2001, text", "text, 2000, text", "1999, text, text, 
text")

extract.year <- function(x, n = 4){
     pattern <- paste(".*([[:digit:]]{", n, "}).*", sep="")
     as.integer(sub(pattern, "\\1", x))
}

extract.year(x)

The argument 'n' is the number of digits of year. Then use the function 
as you want, within lapply, for instance, or directly as in

extract.year(foo$a)

Hope this helps,

Rui Barradas

Em 31-07-2012 16:33, jimi adams escreveu: