[R] Error in rep.int() invalid 'times' value

PIKAL Petr petr.pikal at precheza.cz
Wed Oct 21 09:28:47 CEST 2015


Hi

Several options

Coding in C+ or other similar language (you seem to be more familiar with them)

Using debug and find out how your function behaves in steps

Using function with smaller m, n with Rprof to see where the time is spent.

I believe that coding in R like it was C+ is the way to hell. There is nothing wrong with cycles however if you compute something which can be computed easily by vectorised approach you loose efficiency.

e.g.

you compute this
m<-5
> for ( i in 1:m-1)
+    {
+    b[i]<- (m-i)
+    }
> b
[1] 4 3 2 1

but you can achieve it by

b <-  ((m-1):1)

Time comparison:

> m<-1e5
> system.time(for ( i in 1:m-1) {b[i]<- (m-i)})
   user  system elapsed
  11.61    0.00   12.11
> system.time(bb <- ((m-1):1))
   user  system elapsed
      0       0       0
> all.equal(b,bb)
[1] TRUE

So the time gain is huge only in this small computation.

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 6:33 PM
> To: William Dunlap
> Cc: r-help at r-project.org
> Subject: Re: [R] Error in rep.int() invalid 'times' value
>
> 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]]
>
> ______________________________________________
> 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.


More information about the R-help mailing list