Hi, I've got the following code: p <- 0.34 pb <- p*100 pr <- (1-p)*100 A <- rep(0,pb) # a vector with 34 "zeros" B <- rep(1,pr) # a vector with 66 "ones" Now if I type length(A), R answer correctly 34 but if I type length(B), R answer 65 instead of 66. I don't understand why it happens. Can anyone help me? Thanks in advance. Paolo
Hello, Covelli Paolo wrote:> Hi, > > I've got the following code: > > p <- 0.34 > pb <- p*100 > pr <- (1-p)*100 > > A <- rep(0,pb) # a vector with 34 "zeros" > B <- rep(1,pr) # a vector with 66 "ones"Not true. I counted them myself. There are only 65. I see > pr == 66 [1] FALSE > pr < 66 [1] TRUE So pr must not be what you think it is. For the reason why, see FAQ 7.31.
pr is a numeric number indeed slightly less than 66, hence, the vector generated by rep(1,pr) is of length 65 rather than 66... On Fri, Apr 9, 2010 at 1:58 PM, Covelli Paolo <pcovelli@tele2.it> wrote:> Hi, > > I've got the following code: > > p <- 0.34 > pb <- p*100 > pr <- (1-p)*100 > > A <- rep(0,pb) # a vector with 34 "zeros" > B <- rep(1,pr) # a vector with 66 "ones" > > Now if I type > length(A), R answer correctly 34 > > but if I type > length(B), R answer 65 instead of 66. > > I don't understand why it happens. Can anyone help me? Thanks in advance. > > Paolo > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
See FAQ "7.31 Why doesn't R think these numbers are equal?" and try this: rep(1, ceiling(pr)) On Fri, Apr 9, 2010 at 5:58 PM, Covelli Paolo <pcovelli at tele2.it> wrote:> Hi, > > I've got the following code: > > p <- 0.34 > pb <- p*100 > pr <- (1-p)*100 > > A <- rep(0,pb) ?# a vector with 34 "zeros" > B <- rep(1,pr) ? # a vector with 66 "ones" > > Now if I type > length(A), R answer correctly 34 > > but if I type > length(B), R answer 65 instead of 66. > > I don't understand why it happens. Can anyone help me? Thanks in advance. > > Paolo > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Hi, this is FAQ 7.31: pb and pr are floating-point numbers that are coerced to integer for rep(), and this does not always work the way you want. HTH Stephan Covelli Paolo schrieb:> Hi, > > I've got the following code: > > p <- 0.34 > pb <- p*100 > pr <- (1-p)*100 > > A <- rep(0,pb) # a vector with 34 "zeros" > B <- rep(1,pr) # a vector with 66 "ones" > > Now if I type > length(A), R answer correctly 34 > > but if I type > length(B), R answer 65 instead of 66. > > I don't understand why it happens. Can anyone help me? Thanks in advance. > > Paolo > > ______________________________________________ > 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. >
Covelli, From ?rep Non-integer values of times will be truncated towards zero. If times is a computed quantity it is prudent to add a small fuzz. Thus, the example is a very interesting special case of FAQ 7.31,> sprintf("%17.17a",66)[1] "0x1.08000000000000000p+6"> sprintf("%17.17a",pr)[1] "0x1.07fffffffffff0000p+6">We see that pr is just a tiny bit smaller than 66 and gets rounded down to 65. Rich [[alternative HTML version deleted]]
Hi, On Apr 9, 2010, at 4:58 PM, Covelli Paolo wrote:> Hi, > > I've got the following code: > > p <- 0.34 > pb <- p*100 > pr <- (1-p)*100 >I think you are bumping into the issue that most numbers can't be exactly expressed digitally. Your variable, pr, isn't exactly the 66 you think it is. You'll want to use trunc, round, floor or ceiling depending on what you want to achieve. Personally, I would be tempted to do the integer conversion in one step and then do the balance with integers. p <- 0.34 pb <- trunc(p*100) pr <- 100 - pb There is a FAQ ("7.31 Why doesn't R think these numbers are equal?") that sheds light on this... http://cran.r-project.org/doc/FAQ/R-FAQ.html Cheers, Ben> A <- rep(0,pb) # a vector with 34 "zeros" > B <- rep(1,pr) # a vector with 66 "ones" > > Now if I type > length(A), R answer correctly 34 > > but if I type > length(B), R answer 65 instead of 66. > > I don't understand why it happens. Can anyone help me? Thanks in > advance. > > Paolo > > ______________________________________________ > R-help@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.[[alternative HTML version deleted]]