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

PIKAL Petr petr.pikal at precheza.cz
Tue Oct 20 15:27:52 CEST 2015


Hi

Why are you confused?

> f0(5,3)
     [,1] [,2]
[1,]    0    0
[2,]    1    0
[3,]    2    0
[4,]    0    1
[5,]    1    1
[6,]    0    2
> f1(5,3)
     [,1] [,2]
[1,]    0    0
[2,]    1    0
[3,]    2    0
[4,]    0    1
[5,]    1    1
[6,]    0    2
>

seems to give the same result.

> res0<-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

> res1<-f1(25,15)

So f1 works for required m,n.

William just used different approach to get the same result, which is indeed quite common in R.

Cheers
Petr

From: Maram SAlem [mailto:marammagdysalem at gmail.com]
Sent: Tuesday, October 20, 2015 1:50 PM
To: PIKAL Petr
Cc: r-help at r-project.org
Subject: Re: [R] Error in rep.int() invalid 'times' value

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<mailto: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<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<mailto:r-help at r-project.org>
> Subject: Re: [R] Error in rep.int<http://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<http://rep.int>(rep.int<http://rep.int>(seq_len(nx), rep.int<http://rep.int>(rep.fac, nx)), orep) :
>   invalid 'times' value
> In addition: Warning message:
> In rep.int<http://rep.int>(rep.int<http://rep.int>(seq_len(nx), rep.int<http://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<mailto: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<http://tibco.com>
> >
> > On Thu, Oct 15, 2015 at 7:45 AM, Maram SAlem
> > <marammagdysalem at gmail.com<mailto: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<http://rep.int>(rep.int<http://rep.int>(seq_len(nx), rep.int<http://rep.int>(rep.fac, nx)), orep) :
> >>   invalid 'times' value
> >> In addition: Warning message:
> >> In rep.int<http://rep.int>(rep.int<http://rep.int>(seq_len(nx), rep.int<http://rep.int>(rep.fac, nx)), orep) :
> >>   NAs introduced by coercion to integer range
> >>
> >>
> >> Any help or suggestions will be greatly appreciated.
> >>
> >> Thanks,
> >>
> >> Maram Salem
> >>



________________________________
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]]



More information about the R-help mailing list