Hi This code should explain what I'm trying to do> strptime("30-Jan-08", "%d-%b-%y")[1] "2008-01-30"> > format(strptime("30-Jan-08", "%d-%b-%y") , "%b-%y")[1] "Jan-08"> > strptime(format(strptime("30-Jan-08", "%d-%b-%y") , "%b-%y") ,"%b-%y") [1] NA I have a string of the form "Jan-08" but I can't seem to get strptime to recognise it. The format function can take the date object and using the given flags "%b-%y" create a string. It can't, however, seem to go back the other way, i.e. as in the line below:> strptime(format(strptime("30-Jan-08", "%d-%b-%y") , "%d-%m-%Y"),"%d-%m-%Y") [1] "2008-01-30" Any tips? Thanks Sean> R.Version()$platform [1] "i386-pc-mingw32" $arch [1] "i386" $os [1] "mingw32" $system [1] "i386, mingw32" $status [1] "" $major [1] "2" $minor [1] "6.0" $year [1] "2007" $month [1] "10" $day [1] "03" $`svn rev` [1] "43063" $language [1] "R" $version.string [1] "R version 2.6.0 (2007-10-03)"
On Fri, May 30, 2008 at 12:58:27PM +0100, Creighton, Sean wrote:> Hi > > This code should explain what I'm trying to do > > > strptime("30-Jan-08", "%d-%b-%y") > [1] "2008-01-30" > > > > format(strptime("30-Jan-08", "%d-%b-%y") , "%b-%y") > [1] "Jan-08" > > > > strptime(format(strptime("30-Jan-08", "%d-%b-%y") , "%b-%y") , > "%b-%y") > [1] NA > > > I have a string of the form "Jan-08" but I can't seem to get strptime to > recognise it. The format function can take the date object and using the > given flags "%b-%y" create a string. It can't, however, seem to go back > the other way, i.e. as in the line below: > > > strptime(format(strptime("30-Jan-08", "%d-%b-%y") , "%d-%m-%Y"), > "%d-%m-%Y") > [1] "2008-01-30" > > Any tips?Supply (a|any) date to make it a complete date to parse:> strptime(paste("30", format(strptime("30-Jan-08", "%d-%b-%y") , "%b-%y"), sep="-"), "%d-%b-%y")[1] "2008-01-30" Dirk -- Three out of two people have difficulties with fractions.
As yearmon in the zoo package can convert a month/year to an object of class "yearmon" and that can be converted to other classes, e.g.> library(zoo) > x <- "Jan-08" > as.Date(as.yearmon(x, "%b-%y"))[1] "2008-01-01" On Fri, May 30, 2008 at 7:58 AM, Creighton, Sean <sean.creighton at citi.com> wrote:> Hi > > This code should explain what I'm trying to do > >> strptime("30-Jan-08", "%d-%b-%y") > [1] "2008-01-30" >> >> format(strptime("30-Jan-08", "%d-%b-%y") , "%b-%y") > [1] "Jan-08" >> >> strptime(format(strptime("30-Jan-08", "%d-%b-%y") , "%b-%y") , > "%b-%y") > [1] NA > > > I have a string of the form "Jan-08" but I can't seem to get strptime to > recognise it. The format function can take the date object and using the > given flags "%b-%y" create a string. It can't, however, seem to go back > the other way, i.e. as in the line below: > >> strptime(format(strptime("30-Jan-08", "%d-%b-%y") , "%d-%m-%Y"), > "%d-%m-%Y") > [1] "2008-01-30" > > Any tips? > > Thanks > Sean > > > > > > >> R.Version() > $platform > [1] "i386-pc-mingw32" > > $arch > [1] "i386" > > $os > [1] "mingw32" > > $system > [1] "i386, mingw32" > > $status > [1] "" > > $major > [1] "2" > > $minor > [1] "6.0" > > $year > [1] "2007" > > $month > [1] "10" > > $day > [1] "03" > > $`svn rev` > [1] "43063" > > $language > [1] "R" > > $version.string > [1] "R version 2.6.0 (2007-10-03)" > > ______________________________________________ > 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. >
Reasonably Related Threads
- Microseconds for a zoo object?
- converting character vector "hh:mm" to chron or strptime 24 clock time vectors
- how to convert character string with only month and year into date
- problem with strptime example (PR#811)
- Can strptime handle milliseconds or AM/PM?