

Can anyone think of a slick way to create an array that looks like c(1:n, 1:(n1), 1:(n2), ... , 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


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))
Peter
On Thu, Sep 17, 2015 at 11:19 AM, Dan D < [hidden email]> wrote:
> Can anyone think of a slick way to create an array that looks like c(1:n,
> 1:(n1), 1:(n2), ... , 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
>
>
>
> 
> View this message in context: http://r.789695.n4.nabble.com/c1n1n11n21tp4712390.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


sequence( 5:1)
Regards.
On Thu, Sep 17, 2015 at 11:19:05AM 0700, Dan D wrote:
> Can anyone think of a slick way to create an array that looks like c(1:n,
> 1:(n1), 1:(n2), ... , 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
>
>
>
> 
> View this message in context: http://r.789695.n4.nabble.com/c1n1n11n21tp4712390.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 < [hidden email]> wrote:
>> Can anyone think of a slick way to create an array that looks like c(1:n,
>> 1:(n1), 1:(n2), ... , 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
>>
>>
>>
>> 
>> View this message in context: http://r.789695.n4.nabble.com/c1n1n11n21tp4712390.html>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] mailing list  To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 < [hidden email]> wrote:
>>> Can anyone think of a slick way to create an array that looks like c(1:n,
>>> 1:(n1), 1:(n2), ... , 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
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


I'm not too sure this is any better:
n<5
c<0; # establish result as numeric
for(i in seq(n,1,1)){ c<c(c,seq(1,i)); str(c); }; #generate array
c<c[2:length(c)]; #remove the leading 0
If you're a fan of recursive programming:
> mklist < function(x) { if (x==1) return(1) else return(
c(seq(1,x),mklist(x1)) ) ; }
> mklist(5);
[1] 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
>
Of course, I've not done any error checking in my function definition. And,
for large values, it can nest too deeply and get
Error: evaluation nested too deeply: infinite recursion /
options(expressions=)?
On Thu, Sep 17, 2015 at 1:19 PM, Dan D < [hidden email]> wrote:
> Can anyone think of a slick way to create an array that looks like c(1:n,
> 1:(n1), 1:(n2), ... , 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
>
>
>
> 
> View this message in context:
> http://r.789695.n4.nabble.com/c1n1n11n21tp4712390.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>

Schrodinger's backup: The condition of any backup is unknown until a
restore is attempted.
Yoda of Borg, we are. Futile, resistance is, yes. Assimilated, you will be.
He's about as useful as a wax frying pan.
10 to the 12th power microphones = 1 Megaphone
Maranatha! <><
John McKown
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 < [hidden email]> wrote:
> >>> Can anyone think of a slick way to create an array that looks like c(1:n,
> >>> 1:(n1), 1:(n2), ... , 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
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Very nice variety of solutions to create c(1:n, 1:(n1), 1:(n2), ... , 1)
#Testing the methods with n=1000 (microbenchmark)
n<1000
# by far the nicestlooking, easiest to follow, and fastest is Frank Schwidom's:
# it also requires the minimum amount of memory (as do several of the others)
# 2.73 milliseconds (1x)
sequence(n:1)
# not nearly as nicelooking but almost as fast:
# 2.82 milliseconds (1.03x)
do.call(c, lapply(n:1, function(n1) 1:n1))
# an improvement on look but 5x slower than do.call is:
# 13.3 milliseconds (4.9x)
unlist(lapply(n:1, seq))
## the others are uglier and way slower [uses a full (n+1) x (n+1) matrix]
# 60.8 milliseconds (22.3x)
outer( 1:(n+1), 1:(n+1), '')[ outer( 1:n, 1:(n+1), '>')]
# 71.8 milliseconds (26.3x) [uses a full (n x n) matrix]
junk<array(1:n,dim=c(n,n))
junk[((lower.tri(t(junk),diag=T)))[n:1,]]
# 421.3 milliseconds (154x)
Reduce( function(x,y){c( 1:y, x)}, 1:n)
# 3200 milliseconds (1170x)
cc<0; # establish result as numeric
for(i in seq(n,1,1)){ cc<c(cc,seq(1,i)); str(cc); }; #generate array
cc<cc[2:length(cc)]; #remove the leading 0
} #
# crashes:
mklist < function(n) {
if (n==1) return(1) else return( c(seq(1,n),mklist(n1)) )
}


If you are interested in speed for long input vectors try the following,
which should give the same result as sequence().
mySequence <function (nvec)
{
nvec < as.integer(nvec)
seq_len(sum(nvec))  rep(cumsum(c(0L, nvec[length(nvec)])),
nvec)
}
E.g.,
> n < rpois(1e6, 3)
> system.time(mySequence(n))
user system elapsed
0.07 0.00 0.07
> system.time(sequence(n))
user system elapsed
0.88 0.00 0.87
> identical(mySequence(n), sequence(n))
[1] TRUE
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Thu, Sep 17, 2015 at 12:53 PM, Dan D < [hidden email]> wrote:
> Very nice variety of solutions to create c(1:n, 1:(n1), 1:(n2), ... , 1)
>
> #Testing the methods with n=1000 (microbenchmark)
> n<1000
>
> # by far the nicestlooking, easiest to follow, and fastest is Frank
> Schwidom's:
> # it also requires the minimum amount of memory (as do several of the
> others)
> # 2.73 milliseconds (1x)
> sequence(n:1)
>
> # not nearly as nicelooking but almost as fast:
> # 2.82 milliseconds (1.03x)
> do.call(c, lapply(n:1, function(n1) 1:n1))
>
> # an improvement on look but 5x slower than do.call is:
> # 13.3 milliseconds (4.9x)
> unlist(lapply(n:1, seq))
>
> ## the others are uglier and way slower [uses a full (n+1) x (n+1) matrix]
> # 60.8 milliseconds (22.3x)
> outer( 1:(n+1), 1:(n+1), '')[ outer( 1:n, 1:(n+1), '>')]
>
> # 71.8 milliseconds (26.3x) [uses a full (n x n) matrix]
> junk<array(1:n,dim=c(n,n))
> junk[((lower.tri(t(junk),diag=T)))[n:1,]]
>
> # 421.3 milliseconds (154x)
> Reduce( function(x,y){c( 1:y, x)}, 1:n)
>
> # 3200 milliseconds (1170x)
> cc<0; # establish result as numeric
> for(i in seq(n,1,1)){ cc<c(cc,seq(1,i)); str(cc); }; #generate array
> cc<cc[2:length(cc)]; #remove the leading 0
> } #
>
> # crashes:
> mklist < function(n) {
> if (n==1) return(1) else return( c(seq(1,n),mklist(n1)) )
> }
>
>
>
> 
> View this message in context: http://r.789695.n4.nabble.com/c1n1n11n21tp4712390p4712399.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Note that:
>> Also not sure about efficiency but somewhat shorter...
>> unlist(lapply(5:1, seq))
>>
>>> Peter
>>>
is almost exactly sequence(5:1)
which is
unlist(lapply(5:1,seq_len))
which is "must preferred". See ?seq for details
Cheers,
Bert
>>> On Thu, Sep 17, 2015 at 11:19 AM, Dan D < [hidden email]> wrote:
>>>> Can anyone think of a slick way to create an array that looks like c(1:n,
>>>> 1:(n1), 1:(n2), ... , 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
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 20150917 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 < [hidden email]> wrote:
> > >>> Can anyone think of a slick way to create an array that looks like c(1:n,
> > >>> 1:(n1), 1:(n2), ... , 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
> >
> > ______________________________________________
> > [hidden email] mailing list  To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/rhelp> > PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> > and provide commented, minimal, selfcontained, reproducible code.
> >
>
> ______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

