gpt4 book ai didi

r - 使用字符串向量在数据框中查找字符串

转载 作者:行者123 更新时间:2023-12-04 15:13:42 24 4
gpt4 key购买 nike

晚上好

我有以下数据框:

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 Rgrepl 一起使用

df$found_dishes <- sapply(df[[1]], function(x) any(grepl(list_dishes, x)))

关于r - 使用字符串向量在数据框中查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64731752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com