Thank you so much, Jorge.
I checked and your method is at least 200 times faster than mine!
Exactly what I was looking for.
Dimitri

On Mon, Mar 25, 2013 at 5:49 PM, Jorge I Velez <jorgeivanve...@gmail.com>wrote:

> Hi Dimitri,
>
> If I understood correctly, the following will do:
>
> system.time(sum1 <- apply(mycombos, 1, function(x) sum(values1[x])))
> system.time(sum2 <- apply(mycombos, 1, function(x) sum(values2[x])))
> system.time(sum3 <- apply(mycombos, 1, function(x) sum(values3[x])))
>   cbind(sum1, sum2, sum3)
>
> HTH,
> Jorge.-
>
>
>  On Tue, Mar 26, 2013 at 8:12 AM, Dimitri Liakhovitski <> wrote:
>
>>  This is another method I can think of, but it's also slow:
>>
>> for(i in 1:nrow(mycombos)){  # i=1
>>   indexes=rep(0,10)
>>   myitems<-unlist(mycombos[i,1:4])
>>   indexes[myitems]<-1
>>   mycombos$sum1[i]<-sum(values1 * indexes)
>>   mycombos$sum2[i]<-sum(values2 * indexes)
>>   mycombos$sum3[i]<-sum(values3 * indexes)
>>   }
>>
>>
>> On Mon, Mar 25, 2013 at 5:00 PM, Dimitri Liakhovitski <
>> dimitri.liakhovit...@gmail.com> wrote:
>>
>> > Hello!
>> >
>> > # I have 3 vectors of values:
>> > values1<-rnorm(10)
>> > values2<-rnorm(10)
>> > values3<-rnorm(10)
>> > # In real life, all 3 vectors have a length of 25
>> >
>> > # I create all possible combinations of 4 based on 10 elements:
>> > mycombos<-expand.grid(1:10,1:10,1:10,1:10)
>> > dim(mycombos)
>> > # Removing rows that contain pairs of identical values in any 2 of
>> > these columns:
>> > mycombos<-mycombos[!(mycombos$Var1 == mycombos$Var2),]
>> > mycombos<-mycombos[!(mycombos$Var1 == mycombos$Var3),]
>> > mycombos<-mycombos[!(mycombos$Var1 == mycombos$Var4),]
>> > mycombos<-mycombos[!(mycombos$Var2 == mycombos$Var3),]
>> > mycombos<-mycombos[!(mycombos$Var2 == mycombos$Var4),]
>> > mycombos<-mycombos[!(mycombos$Var3 == mycombos$Var4),]
>> > dim(mycombos)
>> >
>> > # I want to write sums of elements from values1, values2, and values 3
>> > whose numbers are contained in each column of mycombos. Here is how I am
>> > going it now - using a loop:
>> > mycombos$sum1<-NA
>> > mycombos$sum2<-NA
>> > mycombos$sum3<-NA
>> > for(i in 1:nrow(mycombos)){
>> >   mycombos$sum1[i]<-values1[[mycombos[i,"Var1"]]] +
>> > values1[[mycombos[i,"Var2"]]] + values1[[mycombos[i,"Var3"]]] +
>> > values1[[mycombos[i,"Var4"]]]
>> >   mycombos$sum2[i]<-values2[[mycombos[i,"Var1"]]] +
>> > values2[[mycombos[i,"Var2"]]] + values2[[mycombos[i,"Var3"]]] +
>> > values2[[mycombos[i,"Var4"]]]
>> >   mycombos$sum3[i]<-values3[[mycombos[i,"Var1"]]] +
>> > values3[[mycombos[i,"Var2"]]] + values3[[mycombos[i,"Var3"]]] +
>> > values3[[mycombos[i,"Var4"]]]
>> > }
>> > head(mycombos);tail(mycombos)
>> >
>> > # It's going to take me forever with this loop. Is there a faster way of
>> > doing the dame thing? Thanks a lot!
>> >
>> > --
>> > Dimitri Liakhovitski
>> >
>>
>>
>>
>> --
>> Dimitri Liakhovitski
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>


-- 
Dimitri Liakhovitski

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to