[datatable-help] Looking for a faster method

Matthew Dowle mdowle at mdowle.plus.com
Mon Aug 26 01:42:56 CEST 2013


Or a slight refinement of that:

system.time(out2 <- data.table(id=seq(vec2), val=vec2, 
key="val")[J(vec1), list(list(id))]$V1)

Matthew

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20130826/ac6a126d/attachment.html>


More information about the datatable-help mailing list