2016-11-10 75 views
1

我正在與一些R的奇怪行爲作鬥爭。有人可以解釋發生了什麼嗎?檢查向量中是否存在元素R

在以下示例中,check是在第一實例中假,並在第二個真實的。爲什麼seqc不同?

by <- 0.1 
percentage <- 60 

probs <- seq(0,1,by) 
checkValues <- probs * 100 
check <- percentage %in% checkValues 

probs <- c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1) 
checkValues <- probs * 100 
check <- percentage %in% checkValues 

它越來越更加古怪,彷彿我設置by <- 0.25percentage <- 75check屬實

回答

2

你犧牲品floating point error

p1 <- seq(0,1,0.1) 
p2 <- c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1) 
> identical(p1, p2) 
[1] FALSE 
> all.equal(p1, p2) 
[1] TRUE 
+0

'檢查< - as.integer( probs * 100)'修好了,謝謝 – barracuda317