I'm having a strange issues doing what I thought was a relatively simple procedure - I'm trying to subset a numeric vector for a logical qualifier, like so:
> test<-rep(seq(1,10,length.out=20),5)
> head(test)
[1] 1.000000 1.473684 1.947368 2.421053 2.894737 3.368421
> test[test==10]
[1] 10 10 10 10 10
> test[test==1]
[1] 1 1 1 1 1
Super easy, yeah? Here's my vector:
> str(days)
num [1:889] 0.542 0.545 0.549 0.552 0.556 ...
> summary(days)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.5417 1.3130 2.0830 2.0830 2.8540 3.6250
I'm trying to pull out values that match an integer:
> days[days==3]
numeric(0)
That doesn't work, and neither does anything else:
> days[days==0.542]
numeric(0)
> days[days==0.556]
numeric(0)
I imagine I'm doing something pretty simple wrong, but I'm stumped :(
EDIT:
So it looks like it was indeed a floating point number issue, as described by the commenter. 'all.equal' worked in certain situations, but implementing it in all of my code was tiresome. Here's the fix I applied:
days<-round(days,digits=6)
dt.matrix<-apply(dt.matrix,c(1,2),function(x) {round(x,digits=6)} )
I was able to use '==' normally after that, with no issue. I think this worked because small errors were introduced because of other 'apply' functions earlier in my script, and rounding all of the numbers six digits out after the fact seemed to fix it.