多くの文字列が切り捨てられるダーティデータの操作。切り捨てられていない文字列が最も長い新しい変数を作成したいと思います。
入力例:
x <- c("stackoverflow is a great site",
"stackoverflow is a great si",
"stackoverflow is a great",
"stackoverflow is an OK site",
"omg it is friday and so",
"omg it is friday and so sunny",
"arggh how annoying")
必要な出力:
y <- c("stackoverflow is a great site",
"stackoverflow is a great site",
"stackoverflow is a great site",
"stackoverflow is an OK site",
"omg it is friday and so sunny",
"omg it is friday and so sunny",
"arggh how annoying")
検索した後、私が見つけることができる最も近いものはこの質問\回答です類似した文字列のベクトルから一意の文字列を取得します
そのトレッドのさまざまな回答により、切り捨てられた文字列と切り捨てられていない文字列を識別できます。関数の例:
mystringr <- function(x){
x[!sapply(seq_along(x), function(i) any(str_detect(x[-i], x[i])))]
}
x
残りのそれぞれの存在を確認しx
、最も長いものを取得します。
sapply(x, function(s){
temp = x[grepl(s, x)]
temp[which.max(nchar(temp))]
},
USE.NAMES = FALSE)
#[1] "stackoverflow is a great site" "stackoverflow is a great site"
#[3] "stackoverflow is a great site" "stackoverflow is an OK site"
#[5] "omg it is friday and so sunny" "omg it is friday and so sunny"
#[7] "arggh how annoying"
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加