R 将向量元素与另一个向量值匹配

R 将向量元素与另一个向量值匹配,r,R,有人能纠正我试图从与当前值匹配的向量中读取值索引的错误吗。先谢谢你 lat_Array<-c(seq(90,-89.95,by=-0.05)) a<-c(3.7,90) match(a[1],lat_Array) # Returns NA, despite index 1727 having the value 3.7 match(a[2],lat_Array) # Surprisingly works and returns 1 lat_数组首先,请参阅有关浮点数相等的说明

有人能纠正我试图从与当前值匹配的向量中读取值索引的错误吗。先谢谢你

lat_Array<-c(seq(90,-89.95,by=-0.05))
a<-c(3.7,90) 
match(a[1],lat_Array) # Returns NA, despite index 1727 having the value 3.7
match(a[2],lat_Array) # Surprisingly works and returns 1 
lat_数组首先,请参阅有关浮点数相等的说明

查找最接近匹配项的一种方法是:

which.min(abs(lat_Array - a[1]))
##  [1] 1727
lat_Array[1727]
##  [1] 3.7

感谢您指出问题的原因并提供答案:)