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, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862
Murray Jorgensen <maj at stats.waikato.ac.nz> 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 don't think things get more elegant than that. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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.5 2.0 2.5 3.0 3.5 4.0 4.5 > > but this seems rather inelegant. Any suggestions? > > Murray
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 + res[2*j] <- n+j } res + }> interl(4)[1] 1 5 2 6 3 7 4 8> c(x,y)[interl(10)][1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 10.5 You can do some testing to se what is fastest. Kjetil Halvorsen> 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, University of Waikato, Hamilton, New Zealand > Email: maj at waikato.ac.nz Fax 7 838 4155 > Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Murray Jorgensen <maj at stats.waikato.ac.nz> 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 2 5 3 6 This will work for any number of vectors: z <- c(7,8,9) as.vector(rbind(x,y,z)) => 1 4 7 2 5 8 3 6 9 Another approach is this: r <- 1:(2*length(x)) i <- r%%2 r[i == 1] <- x r[i == 0] <- y r => 1 4 2 5 3 6 That generalises too: r <- 1:(3*length(x)) i <- r%%3 r[i == 1] <- x r[i == 2] <- y r[i == 0] <- z r => 1 4 7 2 5 8 3 6 9 I prefer as.vector(rbind(x,y,...))); as it uses fewer R-level operations and should if anything turn over less space, I'd expect it to be faster as well as simpler.
Thanks for the ideas about this problem. It seems mean not to say why I was doing this! I was writing a queue simulation one of whose outputs was a list of pairs (t,n) [event time, number in system after event]. I wanted a plot of the step function showing number in the system as a function of time. I did this by constructing two vectors: one with the event times repeated twice, the other with the number in the system before and after each event (interlacing the lagged numbers with the numbers). Then a simple plot(,,type="l") gives a nice visualization of the queue. Murray -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862