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
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, > ThomasAs 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
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.