Murray Jorgensen <[EMAIL PROTECTED]> wrote:
I want to interlace two vectors.
How, precisely, do you want to do this?
Here are two vectors x and y of the same length:
x <- c(1,2,3)
y <- c(4,5,6)
The simplest way I can think of to interleave them is
as.vector(rbind(x,y))
=> 1 4
On 21 Aug 2003 at 9:16, Murray Jorgensen wrote:
Hola!
I'm not sure if this is better, but if we can interlace first
1:n with (n+1):2n the rest is indexing:
> x <- 1:10
> y <- 1:10 + 0.5
> interl <- function(n) {
+res <- numeric(2*n)
+for (j in 1:n) {
+ res[2*j-1] <- j
+
as.vector(rbind(x,z))or c(rbind(x,y))
saves you one step. Don't know if there's a better solution.
HTH,
Jerome
On August 20, 2003 02:16 pm, Murray Jorgensen wrote:
> I want to interlace two vectors. This I can do:
> > x <- 1:4
> > z <- x+0.5
> > as.vector(t(cbind(x,z)))
>
> [1] 1.0 1
Murray Jorgensen <[EMAIL PROTECTED]> writes:
> I want to interlace two vectors. This I can do:
>
> > x <- 1:4
> > z <- x+0.5
> > as.vector(t(cbind(x,z)))
> [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
>
> but this seems rather inelegant. Any suggestions?
Well, there's as.vector(rbind(x,z)) at least..
I want to interlace two vectors. This I can do:
> x <- 1:4
> z <- x+0.5
> as.vector(t(cbind(x,z)))
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
but this seems rather inelegant. Any suggestions?
Murray
--
Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, Univ