|
Hello,
I wanted to compute the time differenze between to times: first =as.POSIXct( "2012-06-15 16:32:39.0025 CEST") second = as.POSIXct("2012-06-15 16:32:39.0086 CEST") second - first The result is Time difference of 0.006099939 secs instead of just 0.0061 secs So R adds aditional numbers after the result. I know I could round it in this case. But I am working with a large data set and need to always get the correct result. difftime() does not work correct either. Has anybody a suggestion how to get the correct result? Thank you Julia ______________________________________________ [hidden email] 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 Jun 15, 2012, at 12:49 PM, Julia wrote: > Hello, > > I wanted to compute the time differenze between to times: > > first =as.POSIXct( "2012-06-15 16:32:39.0025 CEST") > second = as.POSIXct("2012-06-15 16:32:39.0086 CEST") > second - first > > The result is > Time difference of 0.006099939 secs > > instead of just 0.0061 secs > So R adds aditional numbers after the result. It's a floating point representation issue. You don't really want to change that value, but are asking to see something different: > round ( second - first, 4) Time difference of 0.0061 secs > I know I could round it in this case. > But I am working with a large data set and need to always get the > correct result. > > difftime() does not work correct either. > > Has anybody a suggestion how to get the correct result? Use a computer system that runs on exact arithmetic? Read FAQ 7.31 http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f (And expect to read about 4-6 similar messages in the next hour.) > > Thank you > Julia > > ______________________________________________ > [hidden email] 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. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
In reply to this post by Julia-2
Hi,
I checked the same with strptime. It is the rounding issue. Try this: dat1<-data.frame(datetime=c("2012-06-15 16:32:39.0025 CEST","2012-06-15 16:32:39.0086 CEST")) op<-options(digits.secs=4) dat1$datetime<- strptime(dat1$datetime, "%Y-%m-%d %H:%M:%OS") formatC(as.numeric(difftime(dat1[2,1],dat1[1,1],units="secs")),format="f",digits=4) [1] "0.0061" #or, formatC(as.numeric(diff(dat1$datetime)),format="f",digits=4) [1] "0.0061" A.K. ----- Original Message ----- From: Julia <[hidden email]> To: [hidden email] Cc: Sent: Friday, June 15, 2012 12:49 PM Subject: [R] Wrong computation of time differenze in POSIXct - additional digits Hello, I wanted to compute the time differenze between to times: first =as.POSIXct( "2012-06-15 16:32:39.0025 CEST") second = as.POSIXct("2012-06-15 16:32:39.0086 CEST") second - first The result is Time difference of 0.006099939 secs instead of just 0.0061 secs So R adds aditional numbers after the result. I know I could round it in this case. But I am working with a large data set and need to always get the correct result. difftime() does not work correct either. Has anybody a suggestion how to get the correct result? Thank you Julia ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by David Winsemius
Hello,
A classic of floating-point accuracy is > 3/5 - 3/5 [1] 0 > 3/5 - (2/5 + 1/5) [1] -1.110223e-16 > 3/5 - 2/5 - 1/5 [1] -5.551115e-17 Rui Barradas Em 15-06-2012 18:18, David Winsemius escreveu: > > On Jun 15, 2012, at 12:49 PM, Julia wrote: > >> Hello, >> >> I wanted to compute the time differenze between to times: >> >> first =as.POSIXct( "2012-06-15 16:32:39.0025 CEST") >> second = as.POSIXct("2012-06-15 16:32:39.0086 CEST") >> second - first >> >> The result is >> Time difference of 0.006099939 secs >> >> instead of just 0.0061 secs >> So R adds aditional numbers after the result. > > It's a floating point representation issue. You don't really want to > change that value, but are asking to see something different: > > > round ( second - first, 4) > Time difference of 0.0061 secs > >> I know I could round it in this case. >> But I am working with a large data set and need to always get the >> correct result. >> >> difftime() does not work correct either. >> >> Has anybody a suggestion how to get the correct result? > > Use a computer system that runs on exact arithmetic? > > Read FAQ 7.31 > http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f > > > (And expect to read about 4-6 similar messages in the next hour.) > >> >> Thank you >> Julia >> >> ______________________________________________ >> [hidden email] 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. > > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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 Jun 15, 2012, at 20:33 , Rui Barradas wrote: > Hello, > > A classic of floating-point accuracy is > > > 3/5 - 3/5 > [1] 0 > > 3/5 - (2/5 + 1/5) > [1] -1.110223e-16 > > 3/5 - 2/5 - 1/5 > [1] -5.551115e-17 > > Rui Barradas > Yes. There are only about 16 significant digits in (64 bit) floating point. One further point is that times are stored internally as seconds since Jan 1 1970, of which there has been quite a few by now: > unclass(Sys.time()) [1] 1339793894 with already 10 digits before the decimal point, you can only expect fractional seconds to be accurate to about 6 digits. -pd > Em 15-06-2012 18:18, David Winsemius escreveu: >> >> On Jun 15, 2012, at 12:49 PM, Julia wrote: >> >>> Hello, >>> >>> I wanted to compute the time differenze between to times: >>> >>> first =as.POSIXct( "2012-06-15 16:32:39.0025 CEST") >>> second = as.POSIXct("2012-06-15 16:32:39.0086 CEST") >>> second - first >>> >>> The result is >>> Time difference of 0.006099939 secs >>> >>> instead of just 0.0061 secs >>> So R adds aditional numbers after the result. >> >> It's a floating point representation issue. You don't really want to >> change that value, but are asking to see something different: >> >> > round ( second - first, 4) >> Time difference of 0.0061 secs >> >>> I know I could round it in this case. >>> But I am working with a large data set and need to always get the >>> correct result. >>> >>> difftime() does not work correct either. >>> >>> Has anybody a suggestion how to get the correct result? >> >> Use a computer system that runs on exact arithmetic? >> >> Read FAQ 7.31 >> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f >> >> >> (And expect to read about 4-6 similar messages in the next hour.) >> >>> >>> Thank you >>> Julia >>> >>> ______________________________________________ >>> [hidden email] 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. >> >> David Winsemius, MD >> West Hartford, CT >> >> ______________________________________________ >> [hidden email] 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. > > ______________________________________________ > [hidden email] 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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: [hidden email] Priv: [hidden email] ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
