2016-11-10 101 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