Hello, I am having some fun dealing with dates and times. My input is a excel csv file with two columns with data in the following format: date time 25-Jun-1961 04:00:00 i.e. day - month - year hour:min:sec I would like to have a single object in R that combines these and converts them into a sensible R format (e.g. ISOdatetime(1961,06,25,04,00,00,tz="GMT"). I have played with the function chron and also strptime but can't seem to get them to work. Can anybody help me out please? Thanks Bevare -- View this message in context: http://r.789695.n4.nabble.com/excel-dates-and-times-in-R-tp3720887p3720887.html Sent from the R help mailing list archive at Nabble.com.
Hi> > Hello, > > I am having some fun dealing with dates and times. My input is a excelcsv> file with two columns with data in the following format: > > date time > 25-Jun-1961 04:00:00 > > i.e. day - month - year hour:min:sec > > I would like to have a single object in R that combines these andconverts> them into a sensible R format (e.g. > ISOdatetime(1961,06,25,04,00,00,tz="GMT"). > > I have played with the function chron and also strptime but can't seemto> get them to work.Maybe you did not set appropriate locale. See Sys.getlocale()> Sys.setlocale("LC_TIME","us")[1] "English_United States.1252"> strptime(paste(date, time, sep=" "), format="%d-%b-%Y %H:%M:%S")[1] "1961-06-25 04:00:00">But AFAIK Jun is used in Great Britain similarly like in US so you shall not have problems. Regards Petr> > Can anybody help me out please? > > Thanks > > Bevare > > > -- > View this message in context: http://r.789695.n4.nabble.com/excel-dates- > and-times-in-R-tp3720887p3720887.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Well, strptime is certainly the way to go. You did not provide any reproducible example so i can just roughly point out the way to go: 1: combine the two colums to one with paste(,collapse='_') 2: strptime() with the corresponding formats (like "%d-%b-%Y_%H:%M:%S" or similar) HTH Jannis On 08/05/2011 12:27 PM, bevare wrote:> Hello, > > I am having some fun dealing with dates and times. My input is a excel csv > file with two columns with data in the following format: > > date time > 25-Jun-1961 04:00:00 > > i.e. day - month - year hour:min:sec > > I would like to have a single object in R that combines these and converts > them into a sensible R format (e.g. > ISOdatetime(1961,06,25,04,00,00,tz="GMT"). > > I have played with the function chron and also strptime but can't seem to > get them to work. > > Can anybody help me out please? > > Thanks > > Bevare > > > -- > View this message in context: http://r.789695.n4.nabble.com/excel-dates-and-times-in-R-tp3720887p3720887.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
On 05-Aug-11 10:27:28, bevare wrote:> Hello, > I am having some fun dealing with dates and times. My input > is a excel csv file with two columns with data in the following > format: > > date time > 25-Jun-1961 04:00:00 > > i.e. day - month - year hour:min:sec > > I would like to have a single object in R that combines these > and converts them into a sensible R format (e.g. > ISOdatetime(1961,06,25,04,00,00,tz="GMT"). > > I have played with the function chron and also strptime but > can't seem to get them to work. > > Can anybody help me out please? > > Thanks > BevareI know almost nothing about using the "time" functions in R, but since I see that I get: ISOdatetime(1961,06,25,04,00,00,tz="GMT") # [1] "1961-06-25 04:00:00 GMT" it would seem that you already have it almost made in the original Excel fields, namely: paste("1961-06-25","04:00:00","GMT",sep=" ") # [1] "1961-06-25 04:00:00 GMT" So you could write this as a function getISOdt <- function(date,time,zone){ paste(date,time,zone,sep=" ") } where the parameters date, time and zone are supplied as character strings: getISOdt("1961-06-25","04:00:00","GMT") [1] "1961-06-25 04:00:00 GMT" Then you can apply() this to the two comlumns in the dataframe you get when reading in the Excel CSV file, with zone being supplied independently. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at wlandres.net> Fax-to-email: +44 (0)870 094 0861 Date: 05-Aug-11 Time: 13:59:14 ------------------------------ XFMail ------------------------------