Hi, I have a matlab variable as serial date (class double) in the form 'dd-mmm-yyyy HH:MM:SS'. format long disp( tx(40:60,1) ) 1.0e+05 * 7.356000000813091 7.356000000956856 7.356000001305921 7.356000001654985 7.356000002004049 7.356000002353113 7.356000002702178 7.356000003397179 7.356000004092182 7.356000004787183 7.356000005482185 7.356000006177187 7.356000006940080 7.356000007702975 7.356000008465869 7.356000009228763 7.356000009991657 7.356000010754551 7.356000011517445 7.356000012280339 7.356000013085329 It should be the same as datestr( tx(40:60,1), 0) 01-Jan-2014 00:00:07 01-Jan-2014 00:00:08 01-Jan-2014 00:00:11 01-Jan-2014 00:00:14 01-Jan-2014 00:00:17 01-Jan-2014 00:00:20 01-Jan-2014 00:00:23 01-Jan-2014 00:00:29 01-Jan-2014 00:00:35 01-Jan-2014 00:00:41 01-Jan-2014 00:00:47 01-Jan-2014 00:00:53 01-Jan-2014 00:00:59 01-Jan-2014 00:01:06 01-Jan-2014 00:01:13 01-Jan-2014 00:01:19 01-Jan-2014 00:01:26 01-Jan-2014 00:01:32 01-Jan-2014 00:01:39 01-Jan-2014 00:01:46 01-Jan-2014 00:01:53 I can easily convert it with Matlab but then I will obtain a character format which is useless. I can also make use of cellstr(datestr(tx(:,1), 0)) but then I can't save it in ASCII file. The origin of the Matlab format is supposed to be "0000-00-00". This is the only origin which results in "2014-01-01" which is my actual start date. Can somebody please tell me how I can simply convert serial datetime to datetime in R. Thanks in advance. All the best, Christoph [[alternative HTML version deleted]]
See ?as.Date. I am guessing these are days and fractional days. Try x <- 7.356000000813091e5 as.POSIXct((x - 719529)*86400, origin = "1970-01-01") On 25/06/2014 14:56, Christoph Schl?chter wrote:> Hi, > > I have a matlab variable as serial date (class double) in the form > 'dd-mmm-yyyy HH:MM:SS'. > > format long > disp( tx(40:60,1) ) > > 1.0e+05 * > > 7.356000000813091 > 7.356000000956856 > 7.356000001305921 > 7.356000001654985 > 7.356000002004049 > 7.356000002353113 > 7.356000002702178 > 7.356000003397179 > 7.356000004092182 > 7.356000004787183 > 7.356000005482185 > 7.356000006177187 > 7.356000006940080 > 7.356000007702975 > 7.356000008465869 > 7.356000009228763 > 7.356000009991657 > 7.356000010754551 > 7.356000011517445 > 7.356000012280339 > 7.356000013085329 > > It should be the same as > > datestr( tx(40:60,1), 0) > > 01-Jan-2014 00:00:07 > 01-Jan-2014 00:00:08 > 01-Jan-2014 00:00:11 > 01-Jan-2014 00:00:14 > 01-Jan-2014 00:00:17 > 01-Jan-2014 00:00:20 > 01-Jan-2014 00:00:23 > 01-Jan-2014 00:00:29 > 01-Jan-2014 00:00:35 > 01-Jan-2014 00:00:41 > 01-Jan-2014 00:00:47 > 01-Jan-2014 00:00:53 > 01-Jan-2014 00:00:59 > 01-Jan-2014 00:01:06 > 01-Jan-2014 00:01:13 > 01-Jan-2014 00:01:19 > 01-Jan-2014 00:01:26 > 01-Jan-2014 00:01:32 > 01-Jan-2014 00:01:39 > 01-Jan-2014 00:01:46 > 01-Jan-2014 00:01:53 > > I can easily convert it with Matlab but then I will obtain a character > format which is useless. I can also make use of cellstr(datestr(tx(:,1), > 0)) but then I can't save it in ASCII file. > > The origin of the Matlab format is supposed to be "0000-00-00". This is the > only origin which results in "2014-01-01" which is my actual start date. > > Can somebody please tell me how I can simply convert serial datetime to > datetime in R. > > Thanks in advance. > > All the best, > > Christoph > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- 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
I think the character format for this data is the most versatile and clear option. You do have to prevent the R input function (read.csv? read.table?) from converting it to factor when you read it in, but then you can use as.POSIXct with a format argument (see ?strptime) to obtain useful timestamp values in R. You also need to be clear about time zones, but that is true regardless of the software you use. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On June 25, 2014 6:56:38 AM PDT, "Christoph Schl?chter" <christoph.schlaechter at gmail.com> wrote:>Hi, > >I have a matlab variable as serial date (class double) in the form >'dd-mmm-yyyy HH:MM:SS'. > >format long >disp( tx(40:60,1) ) > >1.0e+05 * > > 7.356000000813091 > 7.356000000956856 > 7.356000001305921 > 7.356000001654985 > 7.356000002004049 > 7.356000002353113 > 7.356000002702178 > 7.356000003397179 > 7.356000004092182 > 7.356000004787183 > 7.356000005482185 > 7.356000006177187 > 7.356000006940080 > 7.356000007702975 > 7.356000008465869 > 7.356000009228763 > 7.356000009991657 > 7.356000010754551 > 7.356000011517445 > 7.356000012280339 > 7.356000013085329 > >It should be the same as > >datestr( tx(40:60,1), 0) > >01-Jan-2014 00:00:07 >01-Jan-2014 00:00:08 >01-Jan-2014 00:00:11 >01-Jan-2014 00:00:14 >01-Jan-2014 00:00:17 >01-Jan-2014 00:00:20 >01-Jan-2014 00:00:23 >01-Jan-2014 00:00:29 >01-Jan-2014 00:00:35 >01-Jan-2014 00:00:41 >01-Jan-2014 00:00:47 >01-Jan-2014 00:00:53 >01-Jan-2014 00:00:59 >01-Jan-2014 00:01:06 >01-Jan-2014 00:01:13 >01-Jan-2014 00:01:19 >01-Jan-2014 00:01:26 >01-Jan-2014 00:01:32 >01-Jan-2014 00:01:39 >01-Jan-2014 00:01:46 >01-Jan-2014 00:01:53 > >I can easily convert it with Matlab but then I will obtain a character >format which is useless. I can also make use of >cellstr(datestr(tx(:,1), >0)) but then I can't save it in ASCII file. > >The origin of the Matlab format is supposed to be "0000-00-00". This is >the >only origin which results in "2014-01-01" which is my actual start >date. > >Can somebody please tell me how I can simply convert serial datetime to >datetime in R. > >Thanks in advance. > >All the best, > >Christoph > > [[alternative HTML version deleted]] > >______________________________________________ >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.
Hi, May be this helps: dat <- read.table(text="7.356000000813091 ? 7.356000000956856 ? 7.356000001305921 ? 7.356000001654985 ? 7.356000002004049 ? 7.356000002353113 ? 7.356000002702178 ? 7.356000003397179 ? 7.356000004092182 ? 7.356000004787183 ? 7.356000005482185 ? 7.356000006177187 ? 7.356000006940080 ? 7.356000007702975 ? 7.356000008465869 ? 7.356000009228763 ? 7.356000009991657 ? 7.356000010754551 ? 7.356000011517445 ? 7.356000012280339 ? 7.356000013085329",sep="",header=F, colClasses="character") library(chron) t1 <- chron(1.0e+05 *as.numeric(dat[,1])) -719529 format(as.POSIXct(paste(as.Date(dates(t1)), times(t1)%%1)),"%m-%b-%Y %H:%M:%S") A.K. On Wednesday, June 25, 2014 10:00 AM, Christoph Schl?chter <christoph.schlaechter at gmail.com> wrote: Hi, I have a matlab variable as serial date (class double) in the form 'dd-mmm-yyyy HH:MM:SS'. format long disp( tx(40:60,1) ) 1.0e+05 * ? 7.356000000813091 ? 7.356000000956856 ? 7.356000001305921 ? 7.356000001654985 ? 7.356000002004049 ? 7.356000002353113 ? 7.356000002702178 ? 7.356000003397179 ? 7.356000004092182 ? 7.356000004787183 ? 7.356000005482185 ? 7.356000006177187 ? 7.356000006940080 ? 7.356000007702975 ? 7.356000008465869 ? 7.356000009228763 ? 7.356000009991657 ? 7.356000010754551 ? 7.356000011517445 ? 7.356000012280339 ? 7.356000013085329 It should be the same as datestr( tx(40:60,1), 0) 01-Jan-2014 00:00:07 01-Jan-2014 00:00:08 01-Jan-2014 00:00:11 01-Jan-2014 00:00:14 01-Jan-2014 00:00:17 01-Jan-2014 00:00:20 01-Jan-2014 00:00:23 01-Jan-2014 00:00:29 01-Jan-2014 00:00:35 01-Jan-2014 00:00:41 01-Jan-2014 00:00:47 01-Jan-2014 00:00:53 01-Jan-2014 00:00:59 01-Jan-2014 00:01:06 01-Jan-2014 00:01:13 01-Jan-2014 00:01:19 01-Jan-2014 00:01:26 01-Jan-2014 00:01:32 01-Jan-2014 00:01:39 01-Jan-2014 00:01:46 01-Jan-2014 00:01:53 I can easily convert it with Matlab but then I will obtain a character format which is useless. I can also make use of cellstr(datestr(tx(:,1), 0)) but then I can't save it in ASCII file. The origin of the Matlab format is supposed to be "0000-00-00". This is the only origin which results in "2014-01-01" which is my actual start date. Can somebody please tell me how I can simply convert serial datetime to datetime in R. Thanks in advance. All the best, Christoph ??? [[alternative HTML version deleted]] ______________________________________________ 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.