[R] read "4-jan-02" as date
bogdan romocea
br44114 at yahoo.com
Wed Oct 13 01:56:21 CEST 2004
Thank you everyone. Indeed, I had read the data via
read.csv and the date column was a factor. Everything works
fine if I convert to character first.
Regards,
b.
--- Sundar Dorai-Raj <sundar.dorai-raj at PDF.COM> wrote:
>
>
> bogdan romocea wrote:
>
> > Dear R users,
> >
> > I have a column with dates (character) in a data frame:
> > 12-Jan-01 11-Jan-01 10-Jan-01 9-Jan-01 8-Jan-01
> 5-Jan-01
> > and I need to convert them to (Julian) dates so that I
> can
> > sort the whole data frame by date. I thought it would
> be
> > very simple, but after checking the documentation and
> the
> > list I still don't have something that works.
> >
> > 1. as.Date returns the error below. What am I doing
> wrong?
> > As far as I can see the character strings are in
> standard
> > format.
> > d$Date <- as.Date(d$Date, format="%d-%b-%y")
> > Error in fromchar(x) : character string is not in a
> > standard unambiguous format
> >
> > 2. as.date {Survival} produces this error,
> > d$Date <- as.date(d$Date, order = "dmy")
> > Error in as.date(d$Date, order = "dmy") : Cannot coerce
> to
> > date format
> >
> > 3. Assuming all else fails, is there a text function
> > similar to SCAN in SAS? Given a string like "9-Jan-01"
> and
> > "-" as separator, I'd like a function that can read the
> > first, second and third values (9, Jan, 01), so that I
> can
> > get Julian dates with mdy.date {survival}.
> >
> > Thanks in advance,
> > b.
> >
>
> If you're reading this from a file (via read.table, for
> example), then
> your date column is probably a factor. Convert to
> character first.
>
> > x
> [1] 12-Jan-01 11-Jan-01 10-Jan-01 9-Jan-01 8-Jan-01
> 5-Jan-01
> Levels: 10-Jan-01 11-Jan-01 12-Jan-01 5-Jan-01 8-Jan-01
> 9-Jan-01
> >
> > Date(x, format="%d-%b-%y")
> Error in fromchar(x) : character string is not in a
> standard unambiguous
> format
> >
> > sort(as.Date(as.character(x), format="%d-%b-%y"))
> [1] "2001-01-05" "2001-01-08" "2001-01-09" "2001-01-10"
> "2001-01-11"
> [6] "2001-01-12"
>
>
> --sundar
>
>
More information about the R-help
mailing list