我有一个字符串列表,我试图查找数据集中的特定列是否包含该字符串。如果是,请将该字符串写在其旁边。
例如。
name<-c("Ann","Brit","Cathy","Debra","Mike","Ram")
brand_detail<-c("Reebok size 10","Nike loafers","Reebok slipper","United Colors of Benetton Shoes","Reebok size 8", "Puma slipper")
data1<- data.frame(name,brand_detail)
brand_list<-c("Reebok", "Nike", "United Colors of Benetton")
我正在使用for循环和放大器的组合应用功能。但是这个循环效率太低,并且因为我有巨大的数据集而需要大量的处理时间
data1$brand_match<-""
for(i in 1:length(brand_list))
{
B <- function(x){grepl(brand_list[i],ignore.case = TRUE,x)}
data1$brand_match[which(lapply(data1$brand_detail,B)=="TRUE")]<-brand_list[i]
}
有没有办法使用嵌套的apply函数或更有效的方法?