[R] Random products of rows in a matrix

arun smartpink111 at yahoo.com
Wed Sep 4 22:05:18 CEST 2013



Hi Edouard,

In terms of speed, your new solution may not be that much different from the old one:

#####large matrix
M<- 10
N<- 1e3
set.seed(249)
A<- matrix(sample(1:10,1e5*4,replace=TRUE),1e5,4)
B<- matrix(NA,ncol=ncol(A),nrow=0)
system.time({
set.seed(54)
for (i in 1 : N) B <- rbind(B, apply(A[sample(1 : nrow(A), M, replace = T),], 2, prod))
})
 # user  system elapsed 
 # 0.240   0.048   0.290 


system.time({
set.seed(54)
res<- do.call(rbind,lapply(1:N,function(x) {A1<-A[sample.int(nrow(A),M,replace=TRUE),]; tapply(as.vector(A1),list(rep(seq_len(ncol(A1)),each=nrow(A1))),prod)}))
})
#  user  system elapsed 
#  0.300   0.000   0.302 


 dimnames(res)<- dimnames(B)
 identical(res,B)
#[1] TRUE

B1<- matrix(NA,ncol=ncol(A),nrow=0)
system.time({
set.seed(54)
for(i in 1:N) {
A1<-A[sample.int(nrow(A),M,replace=TRUE),]
B1<- rbind(B1,tapply(as.vector(A1),list(rep(seq_len(ncol(A1)),each=nrow(A1))),prod))
}
})
# user  system elapsed 
#  0.312   0.008   0.318 


system.time({
set.seed(54)
l<-tapply(rep(M,N),1:N,function(x){A[sample(1:nrow(A), M, replace = T), ]})
B2<-t(sapply(l,apply,2,prod))
})
#  user  system elapsed 
#  0.156   0.136   0.290 

dimnames(B1)<- dimnames(B2)
 identical(B1,B2)
#[1] TRUE
library(matrixStats)
system.time({
set.seed(54)
B3<-do.call(rbind, lapply(1:N, function(x)
colProds(A[sample.int(nrow(A), M, replace=TRUE),])))
})
#  user  system elapsed 
#  0.152   0.000   0.155 
dimnames(B3)<- dimnames(B2)
 all.equal(B2,B3)
#[1] TRUE
A.K.



________________________________
From: Edouard Hardy <hardy.edouard at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Wednesday, September 4, 2013 1:31 PM
Subject: Re: [R] Random products of rows in a matrix



I am not allowed to.
I found a solution :
l<-tapply(rep(M,N),1:N,function(x){A[sample(1 : nrow(A), M, replace = T), ]})
t(sapply(l,apply,2,prod))



Edouard Hardy



On Wed, Sep 4, 2013 at 5:38 PM, arun <smartpink111 at yahoo.com> wrote:

No problem.
>Can I know the reason?
>Tx.
>
>
>
>
>
>
>
>________________________________
>From: Edouard Hardy <hardy.edouard at gmail.com>
>To: arun <smartpink111 at yahoo.com>
>Cc: "dcarlson at tamu.edu" <dcarlson at tamu.edu>
>Sent: Wednesday, September 4, 2013 11:32 AM
>
>Subject: Re: [R] Random products of rows in a matrix
>
>
>
>Hello and thank you for your help.
>Unfortunately, I cannot use any package...
>
>
>
>Edouard Hardy
>
>
>
>On Wed, Sep 4, 2013 at 4:52 PM, arun <smartpink111 at yahoo.com> wrote:
>
>
>>
>>HI Edouard,
>>
>>Is there any limitations in installing a package?
>>
>>Using David's solution, if you could install,
>>library(matrixStats)
>>set.seed(28)
>> A<- matrix(sample(1:10,5*4,replace=TRUE),5,4)
>>
>>B <- matrix(NA, ncol = ncol(A), nrow = 0)
>>N<- 3
>>M<- nrow(A)
>>set.seed(54)
>>
>>for (i in 1 : N) B <- rbind(B, apply(A[sample(1 : nrow(A), M, replace = T),
>>], 2, prod))
>>
>> set.seed(54)
>> B1<- do.call(rbind, lapply(1:N, function(x)
>> colProds(A[sample.int(nrow(A), M, replace=TRUE),])))
>> all.equal(B,B1)
>>#[1] TRUE
>>A.K.
>>
>>
>>
>>
>>----- Original Message -----
>>From: David Carlson <dcarlson at tamu.edu>
>>To: 'Edouard Hardy' <hardy.edouard at gmail.com>; 'R help' <r-help at r-project.org>
>>Cc:
>>Sent: Wednesday, September 4, 2013 10:34 AM
>>Subject: Re: [R] Random products of rows in a matrix
>>
>>Actually you have two loops, the for() loop you created and the
>>loop that is hidden inside apply(). You can hide the first loop
>>with lapply() or sapply():
>>
>>B <- do.call(rbind, lapply(1:N, function(x)
>>colSums(A[sample.int(nrow(A), M, replace=TRUE),])))
>>
>>Or
>>
>>B <- t(sapply(1:N, function(x) colSums(A[sample.int(nrow(A), M,
>>replace=TRUE),])))
>>
>>You could eliminate the apply() loop by taking log(A), using
>>colSums(), and then converting back with exp().
>>
>>-------------------------------------
>>David L Carlson
>>Associate Professor of Anthropology
>>Texas A&M University
>>College Station, TX 77840-4352
>>
>>-----Original Message-----
>>From: r-help-bounces at r-project.org
>>[mailto:r-help-bounces at r-project.org] On Behalf Of Edouard Hardy
>>Sent: Wednesday, September 4, 2013 2:59 AM
>>To: R help
>>Subject: [R] Random products of rows in a matrix
>>
>>Hello everybody,
>>
>>Without any loop and any package,
>>
>>I would like to return N products of M rows in a matrix A :
>>
>>Today, I managed to do it with a loop :
>>
>>B <- matrix(NA, ncol = ncol(A), nrow = 0)
>>for (i in 1 : N) B <- rbind(B, apply(A[sample(1 : nrow(A), M,
>>replace = T),
>>], 2, prod))
>>
>>Do you have a solution ?
>>
>>Thank you in advance !
>>
>>    [[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.
>>
>>______________________________________________
>>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