r - Vectorize which function to get positions -


i have 2 data frames (db1 , db2) , want positions in db2 match arguments in db1. achieve using for loop follows:

db1 <- data.frame(id=rep(1:4,each=4),                   class=sample(1:10, 16, replace=true),                   var=rnorm(16)                   ) db2 <- expand.grid(id=1:4, class=1:10) db2$x <- rnorm(nrow(db2))  for(i in 1:nrow(db1)) print(which(db2$id==db1$id[i] & db2$class==db1$class[i])) 

however loops inefficient vectorizing loop. possible pass vector which() function functions search in db2 each values in db1?

library(data.table) db1 <- data.table(db1) db2 <- data.table(db2) # can index additional columns necessary setkeyv(db1, c("id","class")) setkeyv(db2, c("id","class"))  # show records in db2 match id , class db1  db2[db1,]        id class           x         var  [1,]  1     1 -0.50266835  0.82391749  [2,]  1     9 -1.21245991 -1.43163848  [3,]  1     9 -1.21245991 -0.68622189  [4,]  1    10 -0.28659235 -0.98107793  [5,]  2     4  2.18779836  1.25841256  [6,]  2     6  1.32407301  0.42287395  [7,]  2     7 -0.53808409 -0.12069089  [8,]  2    10 -0.67679146 -0.73930821  [9,]  3     7  0.03133591  0.31142901 [10,]  3     8  0.78927215  1.86952233 [11,]  3     9 -0.04674115 -0.45102021 [12,]  3    10 -0.83388764 -0.04354332 [13,]  4     8  1.17608109 -0.07343352 [14,]  4     8  1.17608109 -0.00053299 [15,]  4     9  0.59344187 -0.21407897 [16,]  4    10 -2.06237055  0.78420146  # return index of matching rows db2[db1, which=t]   [1]  1  9  9 10 14 16 17 20 27 28 29 30 38 38 39 40  # unique row indices > db2[unique(db1),which=t] [1]  1  9 10 14 16 17 20 27 28 29 30 38 39 40 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -