<div>
<div>How about this?<div><br></div><div>system.time(out <- data.table(id=seq(vec2), val=vec2, key="val")[J(vec1)][, list(list(id)), by=val]$V1)</div><div><div> user system elapsed </div><div> 0.098 0.004 0.103 </div></div></div><div></div>
</div>
<div><div><br></div><div>Arun</div><div><br></div></div>
<p style="color: #A0A0A8;">On Sunday, August 25, 2013 at 10:27 PM, arun wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px;">
<span><div><div><div>Hi,</div><div>I tried a ?data.table() method to solve the problem in the link below. </div><div><br></div><div><a href="http://r.789695.n4.nabble.com/how-to-combine-apply-and-which-or-alternative-ways-to-do-so-td4674424.html#a4674434">http://r.789695.n4.nabble.com/how-to-combine-apply-and-which-or-alternative-ways-to-do-so-td4674424.html#a4674434</a></div><div><br></div><div>But, it was not that fast.</div><div><br></div><div>set.seed(24)</div><div>vec1<- sample(1e5,1e3,replace=FALSE)</div><div>set.seed(48)</div><div>vec2<- sample(1e3,1e6,replace=TRUE)</div><div>system.time({res1<- tapply(vec1,1:1e3,FUN=function(i) {which(vec2==i)})})
# user system elapsed </div><div># 3.912 0.000 3.880 </div><div><br></div><div>system.time(res2<- sapply(vec1,function(x) which(vec2%in%x)))</div><div># user system elapsed </div><div># 24.368 0.000 23.247 </div><div>
vecR1<-unlist(res1)</div><div>names(vecR1)<-NULL</div><div>vecR2<- unlist(res2)</div><div>identical(vecR1,vecR2)</div><div>#[1]TRUE</div><div><br></div><div>library(data.table)</div><div>dt1<- data.table(vec1,Group=1:1e3,key='Group')</div><div>system.time({res3<- dt1[,list(list(which(vec1==vec2))),by=Group]})##Not that fast
</div><div># user system elapsed </div><div># 3.756 0.120 3.886 ######</div><div>identical(vecR1,unlist(res3$V1))</div><div>#[1] TRUE</div><div><br></div><div><br></div><div><br></div><div>Is there a faster way?</div><div><br></div><div>Thanks.</div><div><br></div><div>A.K.</div><div>_______________________________________________</div><div>datatable-help mailing list</div><div><a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a></div><div><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></div></div></div></span>
</blockquote>
<div>
<br>
</div>