using regular expression
f<- data.frame(
id=c("A","A","A","A","C","C","D","D","E"),
Mode=c("AK->se","se->AK->gg, bishan->K","AK->se","se->gr->gg, bishan->AK","AK->se","se->gr->gg, bishan->AK","AK->se->AK->gg","se->gr->gg, bishan->AK","se->AK->df, hg->pp->sk")
)
selection = grepl(pattern="^AK->.*AK->",x=f$Mode,perl=TRUE)
f$Mode[selection]
f$id[selection]
using lapply (might be a bit slower if there is a lot of strings)
f<- data.frame(
id=c("A","A","A","A","C","C","D","D","E"),
Mode=c("AK->se","se->AK->gg, bishan->K","AK->se","se->gr->gg, bishan->AK","AK->se","se->gr->gg, bishan->AK","AK->se->AK->gg","se->gr->gg, bishan->AK","se->AK->df, hg->pp->sk")
)
selection = sapply(strsplit(x=f$Mode,split="->"),FUN=function(x) (x[1]=="AK")&(x[length(x)]!="AK")&(sum(x=="AK")>1))
f$Mode[selection]
f$id[selection]