我有以下来自大型 data.table 的示例数据:
ddf = structure(list(id = 1:5, country = c("United States of America",
"United Kingdom", "United Arab Emirates", "Saudi Arabia", "Brazil"
), area = c("North America", "Europe", "Arab", "Arab", "South America"
), city = c("first", "second", "second", "first", "third")), .Names = c("id",
"country", "area", "city"), class = c("data.table", "data.frame"
), row.names = c(NA, -5L))
ddf
id country area city
1: 1 United States of America North America first
2: 2 United Kingdom Europe second
3: 3 United Arab Emirates Arab second
4: 4 Saudi Arabia Arab first
5: 5 Brazil South America third
>
我必须创建一个可以向其发送可变数量的文本参数的函数,并且该函数应该对数据执行 AND 搜索并输出具有所有文本搜索参数的所有行。不同的搜索字符串可以在不同的列中。
例如 searchfn(ddf, 'brazil','third') 应该只打印出最后一行。
这个案子需要被忽略。
数据很大,因此搜索需要快速和速度优化(因此使用 data.table)。
我试过了:
searchfn = function(ddf, ...){
ll = list(...)
print(sapply(ll, function(x) grep(x, ddf, ignore.case=T)))
}
它拾取所有发送的搜索字符串并输出搜索到的数字,但搜索不正确。