我正在尝试创建一个函数search(atr, spec, products)
来搜索来自这样的表中的某些参数:
Zoom Size Resolution
1 12 98 3200
2 15 100 3500
3 10 120 3100
4 8 90 2500
如果属性(atr
)存在,那么它应该显示哪个产品至少具有写入的规范(spec
)。
这就是我所拥有的,但它似乎没有起作用:
my_search = function(atr, spec, products) {
#1 products = data.frame?
if(is.data.frame(products)) {
#2 does atr exist in products?
if(atr %in% names(products)) {
result = products[which(products$spec>=spec),]
return(result)
} else {
print("Sorry, there is no product with that attribute")
}
} else {
print("Sorry, the variable products has to be a data frame")
}
}