数値ベクトルのリストがあり、各ベクトルのコピーを1つだけ含むリストを作成する必要があります。同じ関数のlistメソッドがないので、すべてのベクトルを互いにチェックするために適用する関数を作成しました。
F1 <- function(x){
to_remove <- c()
for(i in 1:length(x)){
for(j in 1:length(x)){
if(i!=j && identical(x[[i]], x[[j]]) to_remove <- c(to_remove,j)
}
}
if(is.null(to_remove)) x else x[-c(to_remove)]
}
問題は、入力リストxのサイズが大きくなると、この関数が非常に遅くなることです。これは、forループによる2つの大きなベクトルの割り当てが原因の1つです。長さ15のベクトルで長さ150万のリストに対して、1分未満で実行されるメソッドを望んでいますが、それは楽観的かもしれません。
リスト内の各ベクトルを他のすべてのベクトルと比較するより効率的な方法を知っている人はいますか?ベクトル自体の長さは同じであることが保証されています。
出力例を以下に示します。
x = list(1:4, 1:4, 2:5, 3:6)
F1(x)
> list(1:4, 2:5, 3:6)