[R] Faster way of summing values up based on expand.grid

arun smartpink111 at yahoo.com
Tue Mar 26 14:39:00 CET 2013


HI,

You could also try this:

set.seed(25)
values1<-rnorm(10)
values2<-rnorm(10)
values3<-rnorm(10) 
mycombos<-expand.grid(1:10,1:10,1:10,1:10) 
mycombos1<- mycombos
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) 
#[1] 5040    4


#the above steps could be collapsed by:


 mycombos2<-mycombos1[rowSums(sapply(as.data.frame(combn(names(mycombos1),2),stringsAsFactors=FALSE),function(x) mycombos1[x[1]]!=mycombos1[x[2]]))==6,]
 dim(mycombos2)
#[1] 5040    4
 head(mycombos2)
#    Var1 Var2 Var3 Var4
#124    4    3    2    1
#125    5    3    2    1
#126    6    3    2    1
#127    7    3    2    1
#128    8    3    2    1
#129    9    3    2    1
 identical(mycombos,mycombos2)
#[1] TRUE
sum1<- apply(mycombos,1,function(x) sum(values1[x])) #Jorge's method

sumNew1<-rowSums(sapply(mycombos2,function(x) values1[x]))
 identical(sumNew1,sum1)
#[1] TRUE
system.time(sum1<- apply(mycombos,1,function(x) sum(values1[x])))
#   user  system elapsed 
#  0.024   0.000   0.026 
 system.time(sumNew1<-rowSums(sapply(mycombos2,function(x) values1[x])))
#   user  system elapsed 
#  0.012   0.000   0.010 

system.time(sumNew2<-rowSums(sapply(mycombos2,function(x) values2[x])))
system.time(sumNew3<-rowSums(sapply(mycombos2,function(x) values3[x])))
cbind(sumNew1,sumNew2,sumNew3)

A.K.

----- Original Message -----
From: Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Monday, March 25, 2013 5:00 PM
Subject: [R] Faster way of summing values up based on expand.grid

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

    [[alternative HTML version deleted]]

______________________________________________
R-help at 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.




More information about the R-help mailing list