ループは必要ない、ちょうどstringrまたはstringiパッケージ内で検索機能を使用しています。
train <- data.frame(subject = c("the box is beauty", "delivery reached on time", "they serve well"),
keyword = c("box", "delivery", "serve"),
stringsAsFactors = FALSE)
library(stringr)
train$position_stringr <- str_locate(train$subject, train$keyword)[,1]
#locate returns a matrix and we are just interested in the start of keyword.
library(stringi)
train$position_stringi <- stri_locate_first(train$subject, regex = train$keyword)[,1]
#locate returns a matrix and we are just interested in the start of keyword.
train
subject keyword position_stringr position_stringi
1 the box is beauty box 5 5
2 delivery reached on time delivery 1 1
3 they serve well serve 6 6
'のgrep(DF $キーワード、DFの$件名)データフレームの最後の行で' – abhiieor
、 'L == C(6、17)'。 'train $ position [3]'を両方に設定しますか?その列にリストが保持されます。 –
grep(df $ keyword、df $ Subject)これは一致します....私は、件名のキーワードの位置を見つけたいと思います。 – Prajna