[18651] in s-news-athena
[S] Summary for 1:n vector
daemon@ATHENA.MIT.EDU (Erin Hodgess)
Fri Jul 16 16:59:16 1999
Date: Fri, 16 Jul 1999 15:54:55 -0500 (CDT)
From: Erin Hodgess <hodgess@uhdux2.dt.uh.edu>
Message-Id: <199907162054.PAA18767@uhdux2.dt.uh.edu>
To: s-news@wubios.wustl.edu
Hello again!
Here is a summary of the wonderful answers that I received:
One possible way is:
n <- 4
N <- matrix(1:n,n,n)
N[lower.tri(N,diag=T)]
> mat<-matrix(0,n,n)
> mat<-row(mat)
> as.vector(mat[lower.tri(mat,diag=T)])
[1] 1 2 3 4 2 3 4 3 4 4
v <- unlist(lapply(1:4, seq, to = 4))
> n <- 4
> x <- matrix (rep(1:n, n), ncol=n)
> x[lower.tri(x, diag=T)]
[1] 1 2 3 4 2 3 4 3 4 4
This is one solution :
vec<-NULL
for(i in 1:n)
vec<-c(vec , i : n )
n <- 4
x <- matrix(1:n,n,n)
x[row(x) >= col(x)]
c(1:n)[c(1:n,2:n,...,n:n)]
> mat_ matrix(rep(1:4,4),ncol=4)
> as.vector(mat[col(mat)<=row(mat)])
[1] 1 2 3 4 2 3 4 3 4 4
myseq_function(n) {
if(n<1) stop("n must be positive")
x_rep.int(1:n,n)
x[x>=rep.int(1:n,rep.int(n,n))]
}
c(outer(1:n,rep(1,n)))[c(lower.tri(diag(n),T))]
> n <- 4
> unlist(lapply(n:1, function(x){seq(to=n, by=1, length=x)}))
[1] 1 2 3 4 2 3 4 3 4 4
x <- row(matrix(0,4,4))
> y <- col(x)
> x
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4
> y
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
[4,] 1 2 3 4
> x_c(x)
> y_c(y)
> x[x >= y]
[1] 1 2 3 4 2 3 4 3 4 4
Let X be and nxn matrix , e.g., n=4:
?t(col(X))[row(X)>=col(X)]
[1] 1 2 3 4 2 3 4 3 4 4
unlist(sapply(1:n, ":", n))
rep(1:n, n)[outer(1:n, 1:n, ">=")]
rep(1:n, n)[rep(1:n, n) >= rep(1:n, rep.int(n, n))]
N <- 1000
vec <- unlist(sapply(1:n,function(x,n)(x:n),n=N))
f0 <- function(n)unlist(lapply(1:n, ":", n))
A faster one is
f2 <- function(n)
{
dx <- rep(as.integer(1), (len <- (n * (n + 1))/2))
dx[1 + len - rev(cumsum(1:(n - 1)))] <- ( - n + 2):0
cumsum(dx)
}
Thank you all again for your help!!!!
Sincerely,
Erin
Hello S+ ers!
Suppose I would like to generate a vector consisting of
1,2,...n,2,3,...n,...n-1,n,n
For example, if n=4, I would like
1,2,3,4,2,3,4,3,4,4
I'm sure there is a quick and simple way to do this.
Note: this is just for an experiment, nothing pressing.
S+ 4.5 on Windows 98.
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news