Thanks William. I've tried the first code, ( with f0() ), but still for n=25, m=15 , I got this:> s<-f0(25,15)Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : invalid 'times' value In addition: Warning message: In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : NAs introduced by coercion to integer range I don't know if this is related to the memory limits of my laptop, or it doesn't have to do with the memory. Any help on how to fix this error will be greatly appreciated. Thanks All. Maram Salem On 15 October 2015 at 17:52, William Dunlap <wdunlap at tibco.com> wrote:> Doing enumerative combinatorics with rejection methods rarely > works well. Try mapping your problem to the problem of choosing > m-1 items from n-1. E.g., your code was > > f0 <- function(n, m) { > stopifnot(n > m) > D<-matrix(0,nrow=n-m+1,ncol=m-1) > for (i in 1:m-1){ > D[,i]<-seq(0,n-m,1) > } > ED <- do.call(`expand.grid`,as.data.frame(D)) > ED<-unname(as.matrix(ED)) > lk<-which(rowSums(ED)<=(n-m)) > ED[lk,] > } > > and I think the following does the same thing in much less space by > transforming the output of combn(). > > f1 <- function(n, m) { > stopifnot(n > m) > r0 <- t(diff(combn(n-1, m-1)) - 1L) > r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1, > len=n-m+1), m-2)) > cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) > } > > The code for adding the last column is a bit clumsy and could probably be > improved. Both f0 and f1 could also be cleaned up to work for m<=2. > > See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more on > this sort of thing. > > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem <marammagdysalem at gmail.com> > wrote: > >> Dear All, >> >> I'm trying to do a simple task (which is in fact a tiny part of a larger >> code). >> >> I want to create a matrix, D, each of its columns is a sequence from 0 to >> (n-m), by 1. Then, using D, I want to create another matrix ED, whose rows >> represent all the possible combinations of the elements of the columns of >> D. Then from ED, I'll select only the rows whose sum is less than or equal >> to (n-m), which will be called the matrix s. I used the following code: >> >> > n=5 >> > m=3 >> > D<-matrix(0,nrow=n-m+1,ncol=m-1) >> > for (i in 1:m-1) >> + { >> + D[,i]<-seq(0,n-m,1) >> + } >> > ED <- do.call(`expand.grid`,as.data.frame(D)) >> > ED<-as.matrix(ED) >> >> > lk<-which(rowSums(ED)<=(n-m)) >> >> > s<-ED[lk,] >> >> >> This works perfectly well. But for rather larger values of n and m (which >> are not so large actually), the number of all possible combinations of the >> columns of D gets extremely large giving me this error (for n=25, m=15): >> >> > ED <- do.call(`expand.grid`,as.data.frame(D)) >> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : >> invalid 'times' value >> In addition: Warning message: >> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : >> NAs introduced by coercion to integer range >> >> >> Any help or suggestions will be greatly appreciated. >> >> Thanks, >> >> Maram Salem >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> > >[[alternative HTML version deleted]]
Hi What about using the second function f1 which William suggested? Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Maram > SAlem > Sent: Tuesday, October 20, 2015 11:06 AM > To: William Dunlap; r-help at r-project.org > Subject: Re: [R] Error in rep.int() invalid 'times' value > > Thanks William. I've tried the first code, ( with f0() ), but still for > n=25, m=15 , I got this: > > > s<-f0(25,15) > Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > invalid 'times' value > In addition: Warning message: > In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > NAs introduced by coercion to integer range > > > I don't know if this is related to the memory limits of my laptop, or > it doesn't have to do with the memory. > > Any help on how to fix this error will be greatly appreciated. > > Thanks All. > > Maram Salem > > On 15 October 2015 at 17:52, William Dunlap <wdunlap at tibco.com> wrote: > > > Doing enumerative combinatorics with rejection methods rarely works > > well. Try mapping your problem to the problem of choosing > > m-1 items from n-1. E.g., your code was > > > > f0 <- function(n, m) { > > stopifnot(n > m) > > D<-matrix(0,nrow=n-m+1,ncol=m-1) > > for (i in 1:m-1){ > > D[,i]<-seq(0,n-m,1) > > } > > ED <- do.call(`expand.grid`,as.data.frame(D)) > > ED<-unname(as.matrix(ED)) > > lk<-which(rowSums(ED)<=(n-m)) > > ED[lk,] > > } > > > > and I think the following does the same thing in much less space by > > transforming the output of combn(). > > > > f1 <- function(n, m) { > > stopifnot(n > m) > > r0 <- t(diff(combn(n-1, m-1)) - 1L) > > r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1, > > len=n-m+1), m-2)) > > cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) } > > > > The code for adding the last column is a bit clumsy and could > probably > > be improved. Both f0 and f1 could also be cleaned up to work for > m<=2. > > > > See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more > > on this sort of thing. > > > > > > > > Bill Dunlap > > TIBCO Software > > wdunlap tibco.com > > > > On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem > > <marammagdysalem at gmail.com> > > wrote: > > > >> Dear All, > >> > >> I'm trying to do a simple task (which is in fact a tiny part of a > >> larger code). > >> > >> I want to create a matrix, D, each of its columns is a sequence from > >> 0 to (n-m), by 1. Then, using D, I want to create another matrix ED, > >> whose rows represent all the possible combinations of the elements > of > >> the columns of D. Then from ED, I'll select only the rows whose sum > >> is less than or equal to (n-m), which will be called the matrix s. I > used the following code: > >> > >> > n=5 > >> > m=3 > >> > D<-matrix(0,nrow=n-m+1,ncol=m-1) > >> > for (i in 1:m-1) > >> + { > >> + D[,i]<-seq(0,n-m,1) > >> + } > >> > ED <- do.call(`expand.grid`,as.data.frame(D)) > >> > ED<-as.matrix(ED) > >> > >> > lk<-which(rowSums(ED)<=(n-m)) > >> > >> > s<-ED[lk,] > >> > >> > >> This works perfectly well. But for rather larger values of n and m > >> (which are not so large actually), the number of all possible > >> combinations of the columns of D gets extremely large giving me this > error (for n=25, m=15): > >> > >> > ED <- do.call(`expand.grid`,as.data.frame(D)) > >> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > >> invalid 'times' value > >> In addition: Warning message: > >> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > >> NAs introduced by coercion to integer range > >> > >> > >> Any help or suggestions will be greatly appreciated. > >> > >> Thanks, > >> > >> Maram Salem > >> > >> [[alternative HTML version deleted]] > >> > >> ______________________________________________ > >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> 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. > >> > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Thanks for the hint Petr. I'm just a little bit confused with the function f1(). could you please help me and insert comments within f1() to be able to relate it with f0()? Thanks a lot. Maram Salem On 20 October 2015 at 11:29, PIKAL Petr <petr.pikal at precheza.cz> wrote:> Hi > > What about using the second function f1 which William suggested? > > Cheers > Petr > > > > -----Original Message----- > > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Maram > > SAlem > > Sent: Tuesday, October 20, 2015 11:06 AM > > To: William Dunlap; r-help at r-project.org > > Subject: Re: [R] Error in rep.int() invalid 'times' value > > > > Thanks William. I've tried the first code, ( with f0() ), but still for > > n=25, m=15 , I got this: > > > > > s<-f0(25,15) > > Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > > invalid 'times' value > > In addition: Warning message: > > In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > > NAs introduced by coercion to integer range > > > > > > I don't know if this is related to the memory limits of my laptop, or > > it doesn't have to do with the memory. > > > > Any help on how to fix this error will be greatly appreciated. > > > > Thanks All. > > > > Maram Salem > > > > On 15 October 2015 at 17:52, William Dunlap <wdunlap at tibco.com> wrote: > > > > > Doing enumerative combinatorics with rejection methods rarely works > > > well. Try mapping your problem to the problem of choosing > > > m-1 items from n-1. E.g., your code was > > > > > > f0 <- function(n, m) { > > > stopifnot(n > m) > > > D<-matrix(0,nrow=n-m+1,ncol=m-1) > > > for (i in 1:m-1){ > > > D[,i]<-seq(0,n-m,1) > > > } > > > ED <- do.call(`expand.grid`,as.data.frame(D)) > > > ED<-unname(as.matrix(ED)) > > > lk<-which(rowSums(ED)<=(n-m)) > > > ED[lk,] > > > } > > > > > > and I think the following does the same thing in much less space by > > > transforming the output of combn(). > > > > > > f1 <- function(n, m) { > > > stopifnot(n > m) > > > r0 <- t(diff(combn(n-1, m-1)) - 1L) > > > r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1, > > > len=n-m+1), m-2)) > > > cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) } > > > > > > The code for adding the last column is a bit clumsy and could > > probably > > > be improved. Both f0 and f1 could also be cleaned up to work for > > m<=2. > > > > > > See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more > > > on this sort of thing. > > > > > > > > > > > > Bill Dunlap > > > TIBCO Software > > > wdunlap tibco.com > > > > > > On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem > > > <marammagdysalem at gmail.com> > > > wrote: > > > > > >> Dear All, > > >> > > >> I'm trying to do a simple task (which is in fact a tiny part of a > > >> larger code). > > >> > > >> I want to create a matrix, D, each of its columns is a sequence from > > >> 0 to (n-m), by 1. Then, using D, I want to create another matrix ED, > > >> whose rows represent all the possible combinations of the elements > > of > > >> the columns of D. Then from ED, I'll select only the rows whose sum > > >> is less than or equal to (n-m), which will be called the matrix s. I > > used the following code: > > >> > > >> > n=5 > > >> > m=3 > > >> > D<-matrix(0,nrow=n-m+1,ncol=m-1) > > >> > for (i in 1:m-1) > > >> + { > > >> + D[,i]<-seq(0,n-m,1) > > >> + } > > >> > ED <- do.call(`expand.grid`,as.data.frame(D)) > > >> > ED<-as.matrix(ED) > > >> > > >> > lk<-which(rowSums(ED)<=(n-m)) > > >> > > >> > s<-ED[lk,] > > >> > > >> > > >> This works perfectly well. But for rather larger values of n and m > > >> (which are not so large actually), the number of all possible > > >> combinations of the columns of D gets extremely large giving me this > > error (for n=25, m=15): > > >> > > >> > ED <- do.call(`expand.grid`,as.data.frame(D)) > > >> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > > >> invalid 'times' value > > >> In addition: Warning message: > > >> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > > >> NAs introduced by coercion to integer range > > >> > > >> > > >> Any help or suggestions will be greatly appreciated. > > >> > > >> Thanks, > > >> > > >> Maram Salem > > >> > > >> [[alternative HTML version deleted]] > > >> > > >> ______________________________________________ > > >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > >> 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. > > >> > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > ________________________________ > Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou > ur?eny pouze jeho adres?t?m. > Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? > neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie > vyma?te ze sv?ho syst?mu. > Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email > jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. > Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi > ?i zpo?d?n?m p?enosu e-mailu. > > V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: > - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? > smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. > - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; > Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany > p??jemce s dodatkem ?i odchylkou. > - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve > v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. > - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za > spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n > nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto > emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich > existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. > > This e-mail and any documents attached to it may be confidential and are > intended only for its intended recipients. > If you received this e-mail by mistake, please immediately inform its > sender. Delete the contents of this e-mail with all attachments and its > copies from your system. > If you are not the intended recipient of this e-mail, you are not > authorized to use, disseminate, copy or disclose this e-mail in any manner. > The sender of this e-mail shall not be liable for any possible damage > caused by modifications of the e-mail or by delay with transfer of the > email. > > In case that this e-mail forms part of business dealings: > - the sender reserves the right to end negotiations about entering into a > contract in any time, for any reason, and without stating any reasoning. > - if the e-mail contains an offer, the recipient is entitled to > immediately accept such offer; The sender of this e-mail (offer) excludes > any acceptance of the offer on the part of the recipient containing any > amendment or variation. > - the sender insists on that the respective contract is concluded only > upon an express mutual agreement on all its aspects. > - the sender of this e-mail informs that he/she is not authorized to enter > into any contracts on behalf of the company except for cases in which > he/she is expressly authorized to do so in writing, and such authorization > or power of attorney is submitted to the recipient or the person > represented by the recipient, or the existence of such authorization is > known to the recipient of the person represented by the recipient. >[[alternative HTML version deleted]]
f0 is essentially your original code put into a function, so expect it to fail in the same way your original code did. f1 should give the same answer as f0, but it should use less memory and time. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Oct 20, 2015 at 2:05 AM, Maram SAlem <marammagdysalem at gmail.com> wrote:> Thanks William. I've tried the first code, ( with f0() ), but still for > n=25, m=15 , I got this: > >> s<-f0(25,15) > Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > invalid 'times' value > In addition: Warning message: > In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > NAs introduced by coercion to integer range > > > I don't know if this is related to the memory limits of my laptop, or it > doesn't have to do with the memory. > > Any help on how to fix this error will be greatly appreciated. > > Thanks All. > > Maram Salem > > On 15 October 2015 at 17:52, William Dunlap <wdunlap at tibco.com> wrote: >> >> Doing enumerative combinatorics with rejection methods rarely >> works well. Try mapping your problem to the problem of choosing >> m-1 items from n-1. E.g., your code was >> >> f0 <- function(n, m) { >> stopifnot(n > m) >> D<-matrix(0,nrow=n-m+1,ncol=m-1) >> for (i in 1:m-1){ >> D[,i]<-seq(0,n-m,1) >> } >> ED <- do.call(`expand.grid`,as.data.frame(D)) >> ED<-unname(as.matrix(ED)) >> lk<-which(rowSums(ED)<=(n-m)) >> ED[lk,] >> } >> >> and I think the following does the same thing in much less space by >> transforming the output of combn(). >> >> f1 <- function(n, m) { >> stopifnot(n > m) >> r0 <- t(diff(combn(n-1, m-1)) - 1L) >> r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1, >> len=n-m+1), m-2)) >> cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) >> } >> >> The code for adding the last column is a bit clumsy and could probably be >> improved. Both f0 and f1 could also be cleaned up to work for m<=2. >> >> See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more on >> this sort of thing. >> >> >> >> Bill Dunlap >> TIBCO Software >> wdunlap tibco.com >> >> On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem <marammagdysalem at gmail.com> >> wrote: >>> >>> Dear All, >>> >>> I'm trying to do a simple task (which is in fact a tiny part of a larger >>> code). >>> >>> I want to create a matrix, D, each of its columns is a sequence from 0 to >>> (n-m), by 1. Then, using D, I want to create another matrix ED, whose >>> rows >>> represent all the possible combinations of the elements of the columns of >>> D. Then from ED, I'll select only the rows whose sum is less than or >>> equal >>> to (n-m), which will be called the matrix s. I used the following code: >>> >>> > n=5 >>> > m=3 >>> > D<-matrix(0,nrow=n-m+1,ncol=m-1) >>> > for (i in 1:m-1) >>> + { >>> + D[,i]<-seq(0,n-m,1) >>> + } >>> > ED <- do.call(`expand.grid`,as.data.frame(D)) >>> > ED<-as.matrix(ED) >>> >>> > lk<-which(rowSums(ED)<=(n-m)) >>> >>> > s<-ED[lk,] >>> >>> >>> This works perfectly well. But for rather larger values of n and m (which >>> are not so large actually), the number of all possible combinations of >>> the >>> columns of D gets extremely large giving me this error (for n=25, m=15): >>> >>> > ED <- do.call(`expand.grid`,as.data.frame(D)) >>> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : >>> invalid 'times' value >>> In addition: Warning message: >>> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : >>> NAs introduced by coercion to integer range >>> >>> >>> Any help or suggestions will be greatly appreciated. >>> >>> Thanks, >>> >>> Maram Salem >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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. >> >> >
Yes Indeed William. f1() works perfectly well and took only 30 secs to execute f1(25,15), but I wonder if there is anyway to speed up the execution of the rest of my code (almost seven hours now) ? Thanks for helping. Maram Salem On 20 October 2015 at 18:11, William Dunlap <wdunlap at tibco.com> wrote:> f0 is essentially your original code put into a function, so > expect it to fail in the same way your original code did. > f1 should give the same answer as f0, but it should use > less memory and time. > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > > On Tue, Oct 20, 2015 at 2:05 AM, Maram SAlem <marammagdysalem at gmail.com> > wrote: > > Thanks William. I've tried the first code, ( with f0() ), but still for > > n=25, m=15 , I got this: > > > >> s<-f0(25,15) > > Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > > invalid 'times' value > > In addition: Warning message: > > In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > > NAs introduced by coercion to integer range > > > > > > I don't know if this is related to the memory limits of my laptop, or it > > doesn't have to do with the memory. > > > > Any help on how to fix this error will be greatly appreciated. > > > > Thanks All. > > > > Maram Salem > > > > On 15 October 2015 at 17:52, William Dunlap <wdunlap at tibco.com> wrote: > >> > >> Doing enumerative combinatorics with rejection methods rarely > >> works well. Try mapping your problem to the problem of choosing > >> m-1 items from n-1. E.g., your code was > >> > >> f0 <- function(n, m) { > >> stopifnot(n > m) > >> D<-matrix(0,nrow=n-m+1,ncol=m-1) > >> for (i in 1:m-1){ > >> D[,i]<-seq(0,n-m,1) > >> } > >> ED <- do.call(`expand.grid`,as.data.frame(D)) > >> ED<-unname(as.matrix(ED)) > >> lk<-which(rowSums(ED)<=(n-m)) > >> ED[lk,] > >> } > >> > >> and I think the following does the same thing in much less space by > >> transforming the output of combn(). > >> > >> f1 <- function(n, m) { > >> stopifnot(n > m) > >> r0 <- t(diff(combn(n-1, m-1)) - 1L) > >> r1 <- rep(seq(from=0, len=n-m+1), choose( seq(to=m-2, by=-1, > >> len=n-m+1), m-2)) > >> cbind(r0[, ncol(r0):1, drop=FALSE], r1, deparse.level=0) > >> } > >> > >> The code for adding the last column is a bit clumsy and could probably > be > >> improved. Both f0 and f1 could also be cleaned up to work for m<=2. > >> > >> See Feller vol. 1 or Benjamin's "Proofs that (really) count" for more on > >> this sort of thing. > >> > >> > >> > >> Bill Dunlap > >> TIBCO Software > >> wdunlap tibco.com > >> > >> On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem <marammagdysalem at gmail.com > > > >> wrote: > >>> > >>> Dear All, > >>> > >>> I'm trying to do a simple task (which is in fact a tiny part of a > larger > >>> code). > >>> > >>> I want to create a matrix, D, each of its columns is a sequence from 0 > to > >>> (n-m), by 1. Then, using D, I want to create another matrix ED, whose > >>> rows > >>> represent all the possible combinations of the elements of the columns > of > >>> D. Then from ED, I'll select only the rows whose sum is less than or > >>> equal > >>> to (n-m), which will be called the matrix s. I used the following code: > >>> > >>> > n=5 > >>> > m=3 > >>> > D<-matrix(0,nrow=n-m+1,ncol=m-1) > >>> > for (i in 1:m-1) > >>> + { > >>> + D[,i]<-seq(0,n-m,1) > >>> + } > >>> > ED <- do.call(`expand.grid`,as.data.frame(D)) > >>> > ED<-as.matrix(ED) > >>> > >>> > lk<-which(rowSums(ED)<=(n-m)) > >>> > >>> > s<-ED[lk,] > >>> > >>> > >>> This works perfectly well. But for rather larger values of n and m > (which > >>> are not so large actually), the number of all possible combinations of > >>> the > >>> columns of D gets extremely large giving me this error (for n=25, > m=15): > >>> > >>> > ED <- do.call(`expand.grid`,as.data.frame(D)) > >>> Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > >>> invalid 'times' value > >>> In addition: Warning message: > >>> In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : > >>> NAs introduced by coercion to integer range > >>> > >>> > >>> Any help or suggestions will be greatly appreciated. > >>> > >>> Thanks, > >>> > >>> Maram Salem > >>> > >>> [[alternative HTML version deleted]] > >>> > >>> ______________________________________________ > >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >>> 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. > >> > >> > > >[[alternative HTML version deleted]]