[R] convert string to date format

arun smartpink111 at yahoo.com
Wed Sep 18 14:35:01 CEST 2013


HI,
May be this helps:
vec1<-as.POSIXlt(strptime(gsub("^\\w+ ","",dates),"%b %d %Y %H:%M:%S"))

dat1<- data.frame(date= gsub("-",".",as.Date(vec1)), time= strftime(vec1,format="%H:%M:%S")
 ,stringsAsFactors=FALSE)
 dat1
#        date     time
#1 2013.09.17 16:25:17
#2 2013.09.18 16:35:17
#3 2013.09.19 16:55:17
A.K.



----- Original Message -----
From: Martin Batholdy <batholdy at googlemail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, September 18, 2013 8:10 AM
Subject: [R] convert string to date format

Hi,

I have a vector that looks like this;
(imported from another file)

dates <- c("Tue Sep 17 2013 16:25:17", "Wed Sep 18 2013 16:35:17", "Thu Sep 19 2013 16:55:17")


now I need a data frame with two columns;

date             time
2013.09.17        16:25:17
2013.09.18        16:35:17
…


I first started with functions like strsplit to do string manipulations.
But since I have to do this for multiple files with different native date-formats, probably there is a more general way to deal with dates and time formats in R.

How can I make R recognize that the dates vector does not contain strings but a dates in a specific format?
And then how can use this to create the mentioned data-frame?


thanks for any suggestions!

______________________________________________
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.




More information about the R-help mailing list