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.liakhovitski@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]]

