[R] c(1:n, 1:(n-1), 1:(n-2), ... , 1)
John McKown
john.archie.mckown at gmail.com
Thu Sep 17 20:57:59 CEST 2015
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(x-1)) ) ; }
> 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 <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
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/c-1-n-1-n-1-1-n-2-1-tp4712390.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
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]]
More information about the R-help
mailing list