2017-02-14 3 views
0

Rと行列ベースのスクリプト言語の方が一般的です。私は、この関数を書いて、他の行のコンテンツと同様の内容を持つ各行のインデックスを返します。これは私が開発している原始的な形のスパム削減です。この関数をさらにベクトル化することはできます

if (!require("RecordLinkage")) install.packages("RecordLinkage") 

library("RecordLinkage") 

# Takes a column of strings, returns a list of index's 
check_similarity <- function(x) { 
    threshold <- 0.8 
    values <- NULL 
    for(i in 1:length(x)) { 
    values <- c(values, which(jarowinkler(x[i], x[-i]) > threshold)) 
    } 
    return(values) 
} 

forループを完全に回避するための方法はありますか?

+1

@akrun、歓声 – user2228313

+0

@Dbいいえ、私は他のすべての行、Xと比較するのです[i]は、[-i] – user2228313

+1

は多分これを試してみてください。X: ' m = as.matrix(sapply(x、jarowinkler、x))> threshold; diag(m)= 0; which(rowSums(m)> 0) '再現可能なデータはありませんが、これはうまくいくと思います。 – dww

答えて

1

sapplyを使用してコードをいくらか簡略化することができます。

# some test data # 
x = c('hello', 'hollow', 'cat', 'turtle', 'bottle', 'xxx') 

# create an x by x matrix specifying which strings are alike 
m = sapply(x, jarowinkler, x) > threshold 

# set diagonal to FALSE: we're not interested in strings being identical to themselves 
diag(m) = FALSE 

# And find index positions of all strings that are similar to at least one other string 
which(rowSums(m) > 0) 
# [1] 1 2 4 5 

e.e.e.これは、 'hello'、 'hollow'、 'turtle'、 'bottle'のインデックス位置を別の文字列に似たものとして返します。

好きな場合は、rowSumsではなくcolSumsを使用して名前付きベクトルを取得できます文字列が長い場合、これは厄介なことができます:更新

which(colSums(m) > 0) 
# hello hollow turtle bottle 
#  1  2  4  5 
関連する問題