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.
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
Works fine when you give as.Date() a character vector. I suspect the Date column in your data frame is a factor. > d <- c("12-Jan-01", "11-Jan-01", "10-Jan-01", "9-Jan-01", "8-Jan-01", "5-Jan-01") > d [1] "12-Jan-01" "11-Jan-01" "10-Jan-01" "9-Jan-01" "8-Jan-01" "5-Jan-01" > as.Date(d, format="%d-%b-%y") [1] "2001-01-12" "2001-01-11" "2001-01-10" "2001-01-09" "2001-01-08" [6] "2001-01-05" > as.Date(factor(d), format="%d-%b-%y") Error in fromchar(x) : character string is not in a standard unambiguous format > Hope this helps, Tony Plate At Monday 09:04 AM 10/11/2004, 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. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Mon, 11 Oct 2004, 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 formatI get:> x <- scan(what="")1: 12-Jan-01 11-Jan-01 10-Jan-01 9-Jan-01 8-Jan-01 5-Jan-01 7: Read 6 items> as.Date(x, format="%d-%b-%y")[1] "2001-01-12" "2001-01-11" "2001-01-10" "2001-01-09" "2001-01-08" [6] "2001-01-05" Are you in an English-speaking domain? Try running in the C locale (?Sys.setlocale). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
>: From: bogdan romocea <br44114 at yahoo.com> : : 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 On Windows I ran it and it seems to work: R> x <- c("12-Jan-01", "11-Jan-01", "10-Jan-01", "9-Jan-01", "8-Jan-01", "5- Jan-01") R> y <- as.Date(x, format="%d-%b-%y") R> y [1] "2001-01-12" "2001-01-11" "2001-01-10" "2001-01-09" "2001-01-08" [6] "2001-01-05" R> R.version.string [1] "R version 2.0.0, 2004-10-04" Perhaps you have read in your data as a factor (which is the default for read.table reading in data frames)? If that is the case, try using as.is = TRUE as an argument to read.table or try this: y <- as.Date(as.character(x), "%d-%b-%y") : 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 as.date requires a character argument. Using x and y from above, either of these should work: as.date(as.character(x)) # or just as.date(x) if x is already character as.date(as.character(y), "ymd") : : 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}. x1 <- sapply(strsplit(as.character(x), split = "-"), function(x) paste(x[2],x[1],x[3],sep="/")) as.date(x1) # convert from dd-mmm-yy to mmm/dd/yy x2 <- sub("(..)-(...)-(..)", "\\2/\\1/\\3", as.character(x)) as.date(x2) : : Thanks in advance, : b. : Check out the article in R News Help Desk in issue 4/1 to learn more about dates. (Click on Newsletter on left side of www.r-project.org.)
bogdan romocea <br44114 at yahoo.com> writes:> 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 formatOdd. Works for me:> as.Date("4-Jan-02", format="%d-%b-%y")[1] "2002-01-04" Which is your R version (and OS, this stuff sometimes relies on system routines)? You do have to watch out for locale dependencies though:> as.Date("4-Okt-02", format="%d-%b-%y")[1] NA and vice versa if you try to read "Oct" in a Danish locale. However, shouldn't give the error message that you see.> 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}.gsub() should do the trick, e.g.:> x[1] "4-jan-02"> gsub("([^-]*)-([^-]*)-([^-]*)", "\\1", x)[1] "4"> gsub("([^-]*)-([^-]*)-([^-]*)", "\\2", x)[1] "jan"> gsub("([^-]*)-([^-]*)-([^-]*)", "\\3", x)[1] "02" -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907