peter dalgaard
2016-Jun-22 09:23 UTC
[R] [FORGED] Re: Generate list if sequence form two vector element
There's also mapply(a, b, FUN=seq, SIMPLIFY=FALSE) (turn off simplication so that you don't unexpectedly get a matrix whenever all elements of results have same length. This also affects apply()-based solutions.) ...except that according to original spec, one should ensure a < b. So myseq <- function(a,b) if(a<b) a:b else b:a mapply(a, b, FUN=myseq, SIMPLIFY=FALSE) -pd> On 22 Jun 2016, at 10:42 , Jim Lemon <drjimlemon at gmail.com> wrote: > > Now why didn't I think of that? > > apply(matrix(c(a,b),ncol=2),1,function(x)x[1]:x[2]) > > Jim > > On Wed, Jun 22, 2016 at 6:14 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote: >> On 22/06/16 20:00, Jim Lemon wrote: >>> >>> Hi Tanvir, >>> Not at all elegant, but: >>> >>> make.seq<-function(x) return(seq(x[1],x[2])) >>> apply(matrix(c(a,b),ncol=2),1,make.seq) >> >> >> Not sure that this is more "elegant" but it's a one-liner: >> >> lapply(1:length(a),function(i,a,b){a[i]:b[i]},a=a,b=b) >> >> cheers, >> >> Rolf >> >> >>> On Wed, Jun 22, 2016 at 5:32 PM, Mohammad Tanvir Ahamed via R-help >>> <r-help at r-project.org> wrote: >>>> >>>> Hi, >>>> I want to do the follow thing >>>> >>>> Input : >>>> a <- c(1,3,6,9) >>>> >>>> >>>> b<-c(10,7,20,2) >>>> >>>> >>>> Expected outcome : >>>> >>>> d<-list(1:10,3:7,6:20,2:9) > > ______________________________________________ > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Keith Jewell
2016-Jun-22 09:54 UTC
[R] [FORGED] Re: Generate list if sequence form two vector element
or as a one-liner mapply(pmin(a, b), pmax(a,b), FUN=seq, SIMPLIFY=FALSE) On 22/06/2016 10:23, peter dalgaard wrote:> There's also > > mapply(a, b, FUN=seq, SIMPLIFY=FALSE) > > (turn off simplication so that you don't unexpectedly get a matrix whenever all elements of results have same length. This also affects apply()-based solutions.) > > ...except that according to original spec, one should ensure a < b. So > > myseq <- function(a,b) if(a<b) a:b else b:a > mapply(a, b, FUN=myseq, SIMPLIFY=FALSE) > > -pd > >> On 22 Jun 2016, at 10:42 , Jim Lemon <drjimlemon at gmail.com> wrote: >> >> Now why didn't I think of that? >> >> apply(matrix(c(a,b),ncol=2),1,function(x)x[1]:x[2]) >> >> Jim >> >> On Wed, Jun 22, 2016 at 6:14 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote: >>> On 22/06/16 20:00, Jim Lemon wrote: >>>> >>>> Hi Tanvir, >>>> Not at all elegant, but: >>>> >>>> make.seq<-function(x) return(seq(x[1],x[2])) >>>> apply(matrix(c(a,b),ncol=2),1,make.seq) >>> >>> >>> Not sure that this is more "elegant" but it's a one-liner: >>> >>> lapply(1:length(a),function(i,a,b){a[i]:b[i]},a=a,b=b) >>> >>> cheers, >>> >>> Rolf >>> >>> >>>> On Wed, Jun 22, 2016 at 5:32 PM, Mohammad Tanvir Ahamed via R-help >>>> <r-help at r-project.org> wrote: >>>>> >>>>> Hi, >>>>> I want to do the follow thing >>>>> >>>>> Input : >>>>> a <- c(1,3,6,9) >>>>> >>>>> >>>>> b<-c(10,7,20,2) >>>>> >>>>> >>>>> Expected outcome : >>>>> >>>>> d<-list(1:10,3:7,6:20,2:9)
Mohammad Tanvir Ahamed
2016-Jun-22 10:28 UTC
[R] [FORGED] Re: Generate list if sequence form two vector element
Thanks everyone for the solutions !! Tanvir Ahamed G?teborg, Sweden | mashranga at yahoo.com ----- Original Message ----- From: Keith Jewell <Keith.Jewell at campdenbri.co.uk> To: r-help at stat.math.ethz.ch Sent: Wednesday, 22 June 2016, 11:54 Subject: Re: [R] [FORGED] Re: Generate list if sequence form two vector element or as a one-liner mapply(pmin(a, b), pmax(a,b), FUN=seq, SIMPLIFY=FALSE) On 22/06/2016 10:23, peter dalgaard wrote:> There's also > > mapply(a, b, FUN=seq, SIMPLIFY=FALSE) > > (turn off simplication so that you don't unexpectedly get a matrix whenever all elements of results have same length. This also affects apply()-based solutions.) > > ...except that according to original spec, one should ensure a < b. So > > myseq <- function(a,b) if(a<b) a:b else b:a > mapply(a, b, FUN=myseq, SIMPLIFY=FALSE) > > -pd > >> On 22 Jun 2016, at 10:42 , Jim Lemon <drjimlemon at gmail.com> wrote: >> >> Now why didn't I think of that? >> >> apply(matrix(c(a,b),ncol=2),1,function(x)x[1]:x[2]) >> >> Jim >> >> On Wed, Jun 22, 2016 at 6:14 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote: >>> On 22/06/16 20:00, Jim Lemon wrote: >>>> >>>> Hi Tanvir, >>>> Not at all elegant, but: >>>> >>>> make.seq<-function(x) return(seq(x[1],x[2])) >>>> apply(matrix(c(a,b),ncol=2),1,make.seq) >>> >>> >>> Not sure that this is more "elegant" but it's a one-liner: >>> >>> lapply(1:length(a),function(i,a,b){a[i]:b[i]},a=a,b=b) >>> >>> cheers, >>> >>> Rolf >>> >>> >>>> On Wed, Jun 22, 2016 at 5:32 PM, Mohammad Tanvir Ahamed via R-help >>>> <r-help at r-project.org> wrote: >>>>> >>>>> Hi, >>>>> I want to do the follow thing >>>>> >>>>> Input : >>>>> a <- c(1,3,6,9) >>>>> >>>>> >>>>> b<-c(10,7,20,2) >>>>> >>>>> >>>>> Expected outcome : >>>>> >>>>> d<-list(1:10,3:7,6:20,2:9)______________________________________________ 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.