Hi Jim, I got an error > df$Date2<- + as.POSIXct(as.character(df$Date1, format = "%m/%d/%Y %H:%M:%S")) Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
On Sun, Feb 2, 2014 at 9:08 PM, Jim Lemon <j...@bitwrit.com.au> wrote: > On 02/03/2014 12:53 PM, Yolande Tra wrote: > >> Hi, >> >> I have the following issue. The dataframe df has a column (Date1) >> supposed >> to be a date but read as a factor. There are two types of values in the >> same column Date1 >> Type 1 are datetime like "5/23/2008 0:00:00" >> Type 2 have no time like "1/10/13". >> >> When I apply the following to the date column >> df$Date1<-as.POSIXct(as.character(df$Date1, format = "%d/%m/%Y")) >> >> For type 1 I got the expected result: "2008-05-23" >> For type 2 I got NA. >> >> I have searched but could not solve it. Please help. >> >> Hi Yolande, > Try this: > > # first get the dates with times > df$Date1<- > as.POSIXct(as.character(df$Date1, format = "%m/%d/%Y %H:%M:%S")) > # then fill in the ones without times > df$Date1[nchar(df$Date1) < 10]<- > as.POSIXct(as.character(df$Date1, format = "%m/%d/%y")) > > Notice that I think you got the month/day order wrong, if your first type > is correct. > > Jim > [[alternative HTML version deleted]] ______________________________________________ R-help@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.