Skip to content

diff question

4 messages · Troels Ring, (Ted Harding), Rolf Turner

#
R version 3.1.1 (2014-07-10) -- "Sock it to Me"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

Dear friends - I have a small problem with diff (I guess)
I made a sequence with fixed interval between consecutive elements - and 
hence thought the  diff would be as specified
but had a vector with apparently identical 12 elements returned from diff
tt <- seq(0,20,by=0.02)
unique(diff(tt)) #[1] 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 
0.02 0.02
Trying to see if these elements in diff were duplicated
duplicated(diff(tt))
#[1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE and from
sum(duplicated(diff(tt)))
[1] 988
saw that 12 of the elements in duplicated(diff(tt)) were FALSE. Would it 
be expected that the first was FALSE and the rest TRUE?
|duplicated()|determines which elements of a vector or data frame are 
duplicates of elements with smaller subscripts, and returns a logical 
vector indicating which elements (rows) are duplicates.

All best wishes
Troels
Aalborg, Denmark
#
Troels, this is due to the usual tiny difference between numbers
as computed by R and the numbers that you think they are!

  tt  <- seq(0,20,by=0.02)
  dtt <- diff(tt)
  length(dtt)
  # [1] 1000
  r02 <- rep(0.02,1000)
  unique(r02 - dtt)
  # [1]  0.000000e+00  3.469447e-18 -3.469447e-18  1.040834e-17
  # [5] -1.734723e-17  3.816392e-17  9.367507e-17  2.046974e-16 
  # [9]  4.267420e-16 -4.614364e-16 -1.349615e-15 -3.125972e-15

Hoping this helps!
Ted.
On 11-Jan-2015 08:29:26 Troels Ring wrote:
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 11-Jan-2015  Time: 08:48:03
This message was sent by XFMail
#
See FAQ 7.31.

cheers,

Rolf Turner
On 11/01/15 21:29, Troels Ring wrote:

  
    
#
I should have added an extra line to the code below, to complete
the picture. Here it is (see below line "##########".
Ted.
On 11-Jan-2015 08:48:06 Ted Harding wrote:
tt  <- seq(0,20,by=0.02)
  dtt <- diff(tt)
  length(dtt)
  # [1] 1000
  r02 <- rep(0.02,1000)
  unique(r02 - dtt)
  # [1]  0.000000e+00  3.469447e-18 -3.469447e-18  1.040834e-17
  # [5] -1.734723e-17  3.816392e-17  9.367507e-17  2.046974e-16
  # [9]  4.267420e-16 -4.614364e-16 -1.349615e-15 -3.125972e-15
  ##########
  sum(dtt != 0.02)
  # [1] 998

So only 2 values among the 1000 in diff(tt) are exactly equal to
[R's representation of] 0.2!
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 11-Jan-2015  Time: 11:41:27
This message was sent by XFMail