从特定数据框列表修改数据框中的值

时间:2020-11-08 16:33:06

标签: r conditional-statements text-classification

我有以下数据。frame

df <- c("sauté de boeuf","sauté de boeuf ou sauté de porc","sauté de boeuf ou omelette")
df <- data.frame(df)

我有一个要在此数据框中标识的关键字列表

motsclés_oeuf <- c("omelette","oeuf","tortilla") 
motsclés_oeuf_ME <-> c("nowords") 
oeuf <- cbind(motsclés_oeuf,motsclés_oeuf_ME)

Motsclés_viande_rouge <- c("boeuf","veau")
Motsclés_viande_rouge_ME <- c("canard","dinde")
viande_rouge <- cbind(Motsclés_viande_rouge,Motsclés_viande_rouge_ME)

Motsclés_viande_blanche <- c("poulet","lapin","porc")
Motsclés_viande_blanche_ME <- c("nowords")
viande_blanche <- cbind(Motsclés_viande_blanche,Motsclés_viande_blanche_ME)

Meat_list <-list(oeuf,viande_rouge,viande_blanche)

我有以下分类程序

classification_function <- function(X,MC,ME){   
  Xsep<-unlist(strsplit(toupper(X)," OU "));
  compteur=0; 
  for(mot in Xsep){
      motsep <- unlist(strsplit(tolower(mot), " "));
      motsep <- lapply(motsep, function(x) sub("(.*)s$", '\\1', x));
      if(length(which(motsep%in%MC))>=1 && length(which(motsep%in%ME))<=0){compteur <- compteur+1}
    }
  return(compteur)
}

output <- vector('list', length(Plat_protidique_liste))
for (i in seq_along(Meat_list)){ 
    output[[i]]  <- data.frame(apply(df, c(1,2), function(x) classification_function(x,Meat_list[[i]][,1],Meat_list[[i]][,2])))} 
}   

但是,我只想将if条件应用于“ viande_rouge”和“ viande_blanche”关键字,而不是“ oeuf”。我应该如何进行? 我应该如何修改if(length(which(motsep%in%MC))>=1 && length(which(motsep%in%ME))<=0){compteur <- compteur+1}

谢谢您的帮助。

0 个答案:

没有答案