[R] Extracting Year (only) information from Non-Standard Dates

jim holtman jholtman at gmail.com
Sun Dec 16 17:07:56 CET 2007


try this:

> dates <- c("January 12, 1988", "March 4, 2006", "1958")
> gsub(".*, *(.*)", "\\1", dates) # notice the it is comma blank asterisk in the string
[1] "1988" "2006" "1958"



On Dec 16, 2007 7:34 AM, Anupa Fabian <anupa.fabian at yahoo.com> wrote:
> I have some data whose date column consists of two types of date entries:
> (a) year-only entries (eg "1983") and
> (b) full date info (eg September 12, 1962).
>
> Here's what the non-standard date info looks like:
> > mode(non.standard.dates)
> [1] "numeric"
> > head(non.standard.dates)
> [1] July 15, 1925     February 13, 1923 July 10, 1988     February 24, 1931
> [5] 1952     January 9, 1957
> 1638 Levels: 1732 1735 1736 1740 1745 1748 1749 1750 1752 1754 1757 1760 ... September 9, 1898
>
> I just want to extract out the year information (viz in the example above: 1925, 1923, 1988, 1931, 1952 and 1957) and create a "year" column. Have tried using the R date libraries without success, but they seem more designed for working with dates which are standardized the same way.... I'd really appreciate suggestions from more experienced R folks. Thank you.
>
>
>
>
>      ____________________________________________________________________________________
> Never miss a thing.  Make Yahoo your home page.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list