晚上好
我有以下数据框:
df <- data.frame(I(list(c("nugget de blé","boeuf"),"nugget de blé")))
list_dishes <- c("nugget de blé")
我想在 list_dishes 中识别 df 的菜品:当识别到一个菜品时计数 1。
我写了下面的算法,但它没有字。基本上,算法仅在“nugget the blé”单独出现时才进行识别。
classification <- function(X,list){
compteur = 0
if(length(which(X%in%list)>=1){compteur = compteur + 1}
return(compteur)
}
results <- data.frame(apply(df, c(1,2), function(x) calcul(x,list_dishes)))
你能帮帮我吗
谢谢
最佳答案
我们可以将 sapply
与来自 base R
的 grepl
一起使用
df$found_dishes <- sapply(df[[1]], function(x) any(grepl(list_dishes, x)))
关于r - 使用字符串向量在数据框中查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64731752/