Re: [R] year extraction over a list

2012-07-31 Thread Rui Barradas
Hello, Ok, try this, then. (I've renamed the function and included some numbers in the text strings.) x <- c("text, text, 2001, text, 1234", "text, 2000, 1234, text", "1999, text, 1234, text, text") extract.first.year <- function(x, n = 4){ pattern <- paste("\\D*(\\d{", n, "}).*$", sep=""

Re: [R] year extraction over a list

2012-07-31 Thread arun
Hello, Try this: list1<-list( "text, text, 2001, text", "text, 2000, text",  "1999, text, text, text") l1<-lapply(list1,function(x) gsub("\\D","",x)) [[1]] [1] "2001" [[2]] [1] "2000" [[3]] [1] "1999" unlist(l1) [1] "2001" "2000" "1999" A.K. - Original Message - From: jimi ad

Re: [R] year extraction over a list

2012-07-31 Thread Rui Barradas
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