Created
February 14, 2016 23:34
-
-
Save infotroph/7609f1a0bca9aa157020 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
strs = c("nomatch", "Odum 1969", "2001FACE_3b", "1800", "sdfghj1928qwer") | |
strs_yrs = gsub(".*((19|20)[0-9]{2}).*", "\\1", strs) | |
strs_yrs | |
# [1] "nomatch" "1969" "2001" "1800" "1928" | |
# Oops, strings with no year (or year from wrong century) are returned unchanged! Let's remove them separately: | |
strs_haveyr = grepl("(19|20)[0-9]{2}", strs) | |
strs_yrs[!strs_haveyr] = "" | |
strs_yrs | |
# [1] "" "1969" "2001" "" "1928" | |
# (N.B. assumes only one year per string -- if several, will return only 1st one.) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! I'm just trying it now.
*** This works great *** Just saved me going through a long list of differently formatted references to extract the year of publication.