[R] year extraction over a list

arun smartpink111 at yahoo.com
Tue Jul 31 20:01:14 CEST 2012


Try this:

"text, text, 2001, text",
"text, 2000, text",
 "1999, text, text, text")
l1<-lapply(list1,function(x) gsub("\\D","",x))
[1] "2001"

[1] "2000"

[1] "1999"

[1] "2001" "2000" "1999"


----- Original Message -----
From: jimi adams <jimi.adams at gmail.com>
To: r-help at r-project.org
Sent: Tuesday, July 31, 2012 11:33 AM
Subject: [R] year extraction over a list

I have a data frame, one element in that data frame is a LIST, with each element being a character string. I am trying to extract the first year listed in each of those character strings. The character elements are typically csv, but the position of the year can vary (think citations with varying citation standards). I.e., 

[1] text, text, 2001, text
[2] text, 2000, text
[3] 1999, text, text, text, …

I'm trying to figure out how to create a new list such that each element is that year, i.e., the result on the above would be:
[1] 2001
[2] 2000 
[3] 1999

For some reason i'm not figuring out how to properly get lapply and strsplit (or other alternatives) to play nicely together. Any help greatly appreciated.


jimi adams
Assistant Professor
Department of Sociology
American University
e: jadams at american.edu
w: jimiadams.com

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list