[R] Help with dates and characters

Marc Schwartz marc_schwartz at me.com
Tue Jun 29 17:01:32 CEST 2010


On Jun 29, 2010, at 8:36 AM, 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


As is typically the case with R, there is more than one possible solution:

> x
[1] "2000-01-01" "2000-01-23" "2001-03-12"

 
# See ?substr
> substr(x, 1, 7)
[1] "2000-01" "2000-01" "2001-03"


# See ?sub and ?regex
# Replace the trailing '-' and 2 occurrences of 0-9
# with "". The '$' indicates the end of the string
> sub("-[0-9]{2}$", "", x)
[1] "2000-01" "2000-01" "2001-03"


Each of the above return a character vector, not a "Date" class object.

If you actually want the vector as a Date class object, but just 'format' the output as YY-MM, then you can use:

# See ?as.Date and ?strptime
> format(as.Date(x, format = "%Y-%m-%d"), "%Y-%m")
[1] "2000-01" "2000-01" "2001-03"


HTH,

Marc Schwartz



More information about the R-help mailing list