This is just a little comment to supplement Michael's excellent solution. If there are even a few (e.g., 5 each) repeated values, this:
as.POSIXct(as.character(levels(x)), format = "%d/%m/%Y %H:%M")[x] will be substantially faster, with the speed gains strongly associated with the number of replicates in your factor (where 'x' is your factor (note it is used twice!!). A little timing example: x <- factor(rep(paste("01/01/2012 10:", 0:59, sep = ''), each = 5)) t1 <- t2 <- NULL system.time(replicate(500, {t1 <<- as.POSIXct(as.character(x), format = "%d/%m/%Y %H:%M")})) system.time(replicate(500, {t2 <<- as.POSIXct(as.character(levels(x)), format = "%d/%m/%Y %H:%M")[x]})) all.equal(t1, t2) #### on my machine ## > system.time(replicate(500, {t1 <<- as.POSIXct(as.character(x), format = "%d/%m/%Y %H:%M")})) ## user system elapsed ## 3.01 0.01 3.03 ## > system.time(replicate(500, {t2 <<- as.POSIXct(as.character(levels(x)), format = "%d/%m/%Y %H:%M")[x]})) ## user system elapsed ## 0.67 0.00 0.67 ## > all.equal(t1, t2) ## [1] TRUE On Tue, Mar 13, 2012 at 9:30 AM, R. Michael Weylandt <michael.weyla...@gmail.com> wrote: > as.POSIXct(as.character(FACTORHERE), format = "%d/%m/%Y %H:%M") > > Michael > > On Tue, Mar 13, 2012 at 12:20 PM, Haojie Yan <yhj...@googlemail.com> wrote: >> Dear R-user, >> >> I have read a dataset from .csv file into R. This dataset includes one >> column containing some data in 'date and time' format, e.g. 'dd/mm/yyyy >> hh:mm'. >> >> These data were automatically read and saved as 'factor' in R. When I was >> trying to produce some plots (such as time series) with the above 'date and >> time' on x-axis, it caused some disodering problem, e.g. 1st of March 2012 >> is in front of 10th of Feb. 2012 (if the data is from 10th Feb. 2012 to 1st >> of March 2012). I understand that I might have to convert them from >> 'factor' to 'date' first, so I tried using 'as.date'. But this method seems >> only work for data in format of 'd/m/y' and no further option that allows >> me to add hours and minutes. >> >> I checked online for other methods such as 'as.POSIX' and 'strptime' but >> none of them seem to offer me a quick solution. >> >> Please note that the data I received is recorded every 10 minutes so they >> are saved in the form of 'dd/mm/yyyy hh:mm', e.g. I only have data >> measured up to 'minute' NOT to 'second'. Are there any direct solution >> that I can solve this issue?? >> >> >> Many thanks in advance! >> HJ >> >> [[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. > > ______________________________________________ > 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. -- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/ ______________________________________________ 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.