[R] c(1:n, 1:(n-1), 1:(n-2), ... , 1)

Frank Schwidom schwidom at gmx.net
Tue Sep 22 22:34:53 CEST 2015


Hi

I have to correct myself, this last solution is not universally valid

here a better one:

tmp1 <- ( 1 - outer( max( x):1, x, FUN='-'))
tmp1[ tmp1 > 0]


On 2015-09-17 21:06:30, Frank Schwidom wrote:
> 
> how abount a more complicated one?
> 
> outer( 1:5, 1:5, '-')[ outer( 1:5, 1:5, '>')]
>  [1] 1 2 3 4 1 2 3 1 2 1
> 
> 
> On Thu, Sep 17, 2015 at 11:52:27AM -0700, David Winsemius wrote:
> > You can add this to the list of options to be tested, although my bet would be placed on `sequence(5:1)`:
> > 
> > > Reduce( function(x,y){c( 1:y, x)}, 1:5)
> >  [1] 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
> > 
> > 
> > On Sep 17, 2015, at 11:40 AM, Achim Zeileis wrote:
> > 
> > > On Thu, 17 Sep 2015, Peter Langfelder wrote:
> > > 
> > >> Not sure if this is slicker or easier to follow than your solution,
> > >> but it is shorter :)
> > >> 
> > >> do.call(c, lapply(n:1, function(n1) 1:n1))
> > > 
> > > Also not sure about efficiency but somewhat shorter...
> > > unlist(lapply(5:1, seq))
> > > 
> > >> Peter
> > >> 
> > >> On Thu, Sep 17, 2015 at 11:19 AM, Dan D <ddalthorp at usgs.gov> wrote:
> > >>> Can anyone think of a slick way to create an array that looks like c(1:n,
> > >>> 1:(n-1), 1:(n-2), ... , 1)?
> > >>> 
> > >>> The following works, but it's inefficient and a little hard to follow:
> > >>> n<-5
> > >>> junk<-array(1:n,dim=c(n,n))
> > >>> junk[((lower.tri(t(junk),diag=T)))[n:1,]]
> > >>> 
> > >>> Any help would be greatly appreciated!
> > >>> 
> > >>> -Dan
> > >>> 
> > >>> 
> > 
> > David Winsemius
> > Alameda, CA, USA
> > 
> > ______________________________________________
> > 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.
> >
> 
> ______________________________________________
> 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.
>



More information about the R-help mailing list