Hi I need to use rep() to get a vector out, but I have spotted something very strange. See the reproducible example below. N <- 79 seg <- 5 segN <- N / seg # = 15.8 d1 <- seg - ( segN - floor(segN) ) * seg d1 # = 1 rep(2, d1) # = numeric(0), strange - why doesn't it print one "2"? rep(2, 1) # 2, ok rep(2, d1 / 1,1) # 2, this does work rep(2, d1 + 2) # "2 2" - also works but... d1 + 2 # = 3! so why does it print two 2s above? d1 == 1 # FALSE all.equal(d1, 1) # TRUE identical(d1, 1) # FALSE Try something else... d2 <- 4 - ( (79/4) - floor(79/4))* 4 d2 # = 1 rep(2, d2) # 2 : this works! d2 == 1 # TRUE all.equal(d2, 1) # TRUE identical(d2, 1) # TRUE #version info platform x86_64-pc-linux-gnu arch x86_64 os linux-gnu system x86_64, linux-gnu status major 2 minor 10.1 year 2009 month 12 day 14 svn rev 50720 language R version.string R version 2.10.1 (2009-12-14) Seems like there's some binary maths errors here somewhere. Very strange to me. Anyway, I need to be able to use the result d1 in a rep() command. Any way to force rep not to be *too* specific in how it reads its "times" argument? Thanks in advance, Benjamin Hoyer DTU MSc Mathematical Modelling (2012) / Novozymes student helper [[alternative HTML version deleted]]
Not so strange, in fact this is FAQ 7.31, and has to do (as you guess) with the way that computers store numbers. You need to do as you did, and use round() or floor() or similar to ensure that you get the results you expect. Sarah 2011/9/12 Benjamin H?yer <bdhoyer at gmail.com>:> Hi > > I need to use rep() to get a vector out, but I have spotted something very > strange. ?See the reproducible example below. > > N <- 79 > seg <- 5 > segN <- N / seg ? # = 15.8 > > d1 <- seg - ( segN - floor(segN) ) * seg > d1 ? ? ? ? ? ? ? ? ? ?# = 1 > > rep(2, d1) ? ? ? ? ?# = numeric(0), strange - why doesn't it print one "2"? > rep(2, 1) ? ? ? ? ? ?# 2, ok > rep(2, d1 / 1,1) ? # 2, this does work > rep(2, d1 + 2) ? ?# "2 2" - also works but... > d1 + 2 ? ? ? ? ? ? ?# = 3! so why does it print two 2s above? > > d1 == 1 ? ? ? ? ? ? # FALSE > all.equal(d1, 1) ? # TRUE > identical(d1, 1) ? # FALSE > > Try something else... > > d2 <- 4 - ( (79/4) - floor(79/4))* 4 > d2 ? ? ? ? ? ? ? ? ? ? # = 1 > > rep(2, d2) ? ? ? ? ?# 2 : this works! > > d2 == 1 ? ? ? ? ? ? # TRUE > all.equal(d2, 1) ? # TRUE > identical(d2, 1) ? # TRUE > > #version info > platform ? ? ? x86_64-pc-linux-gnu > arch ? ? ? ? ? x86_64 > os ? ? ? ? ? ? linux-gnu > system ? ? ? ? x86_64, linux-gnu > status > major ? ? ? ? ?2 > minor ? ? ? ? ?10.1 > year ? ? ? ? ? 2009 > month ? ? ? ? ?12 > day ? ? ? ? ? ?14 > svn rev ? ? ? ?50720 > language ? ? ? R > version.string R version 2.10.1 (2009-12-14) > > > Seems like there's some binary maths errors here somewhere. ?Very strange to > me. ?Anyway, I need to be able to use the result d1 in a rep() command. ?Any > way to force rep not to be *too* specific in how it reads its "times" > argument? > > Thanks in advance, > Benjamin Hoyer >-- Sarah Goslee http://www.functionaldiversity.org
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Benjamin H?yer > Sent: Monday, September 12, 2011 6:19 AM > To: r-help at r-project.org > Cc: tapo at novozymes.com > Subject: [R] 1 not equal to 1, and rep command > > Hi > > I need to use rep() to get a vector out, but I have spotted something very > strange. See the reproducible example below. > > N <- 79 > seg <- 5 > segN <- N / seg # = 15.8 > > d1 <- seg - ( segN - floor(segN) ) * seg > d1 # = 1Not on my machine.> d1-1[1] -3.552714e-15 See FAQ 7.31> > rep(2, d1) # = numeric(0), strange - why doesn't it print one > d1-1[1] -3.552714e-15 <<<snip>>>> > > Seems like there's some binary maths errors here somewhere. Very strange > to > me. Anyway, I need to be able to use the result d1 in a rep() command. > Any > way to force rep not to be *too* specific in how it reads its "times" > argument?Maybe round() will work for you, since you seem to be expecting a whole number. rep(2, round(d1)) Hope this is helpful. Dan Daniel Nordlund Bothell, WA USA