事情是這樣的,也許:
get_rest <- function(state, rating) {
rest_data[rest_data$state == state & rest_data$rating == rating, 'restaurant_name']
}
get_rest('NY', 4.9)
#[1] e
而實際上這是一種更好的方法來測試它:
#almost equal is a vectorised form of all.equal that
#checks if two numbers are equal but with a tolerance level
#because of the inconistenies of storing numbers in a computer
#check: http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal
#for details
almost.equal <- function (x, y, tolerance=.Machine$double.eps^0.5,
na.value=TRUE)
{
answer <- rep(na.value, length(x))
test <- !is.na(x)
answer[test] <- abs(x[test] - y) < tolerance
answer
}
get_rest <- function(state, rating) {
rest_data[rest_data$state == state & almost.equal(rest_data$rating, rating),
'restaurant_name']
}
get_rest('NY', 4.9)
#[1] e
我從here
被盜
almost.equal
將評分存儲爲字符或事實可能更有意義r數據,如果只有1個小數點的特異性。這將避免使用數字精度公差來解決問題。 – thelatemail是的,這是非常真實的。我實際上是想把這張貼作爲我的初步答案,但後來我決定使用'almost.equal'(作爲一個通用選項)可能會更好。非常有效的評論,感謝@thelatemail – LyzandeR
或者就此而言,將其存儲爲0到50之間的整數,並確保所有比較都是整數到整數。 – thelatemail