[R] Help with dates and characters

Allan Engelhardt allane at cybaea.com
Tue Jun 29 17:01:42 CEST 2010


If the vector elements are (still) strings, you could simply try

x<- c("2000-01-01", "2000-01-23", "2001-03-12", "2009-12-31")
substring(x, 1, 7)
# [1] "2000-01" "2000-01" "2001-03" "2009-12"


Hope this helps a little.

Allan



On 29/06/10 14:36, Thomas Jensen wrote:
> Dear R Experts,
>
> I have a vector of dates in character format like this:
>
> date
> "2000-01-01"
> "2000-01-23"
> "2001-03-12"
> ...
> ...
> ...
> "2009-12-31"
>
> I would like to delete the last part of the character string (i.e. the 
> "day" part), so the vector looks like this:
>
> date
> "2000-01"
> "2000-01"
> "2001-03"
> ...
> ...
> ...
> "2009-03"
>
> I have been looking into regular expressions, but i find this very 
> confusing.
>
> Thank you for your help,
> Thomas
>
> ______________________________________________
> 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.



More information about the R-help mailing list