Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
2 つの行列 A と B を考えてみましょう。A は B のサブセットです。行列 B で A の各行のインデックスを見つける方法は? 再現可能な例を次に示します。
set.seed(30) B <- matrix(rnorm(n =30,mean = 0), ncol=3) A <- subset(B, B[,1] > 1)
目標はidx、この場合は行 4 と 5 を与えるインデックスを見つけることです。
idx
> match(apply(A, 1, paste, collapse="\b"), apply(B, 1, paste, collapse="\b")) [1] 4 5
または、次のようにすることもできます。
transform(A, idx = 1 * duplicated(rbind(A, B))[-seq_len(nrow(A))])
@Arunによる、適用なしの優れたソリューション。