Alternate, Interweave or Interlace Two Vectors

Alternate, interweave or interlace two vectors

Your rbind method should work well. You could also use

rpois(lambda=c(3,4),n=1e6)

because R will automatically replicate the vector of lambda values to the required length. There's not much difference in speed:

library(rbenchmark)
benchmark(rpois(1e6,c(3,4)),
c(rbind(rpois(5e5,3),rpois(5e5,4))))


# test replications elapsed relative
# 2 c(rbind(rpois(5e+05, 3), rpois(5e+05, 4))) 100 23.390 1.112168
# 1 rpois(1e+06, c(3, 4)) 100 21.031 1.000000

and elegance is in the eye of the beholder ... of course, the c(rbind(...)) method works in general for constructing alternating vectors, while the other solution is specific to rpois or other functions that replicate their arguments in that way.

Interlacing two vectors

You can rbind them, then coerce back to a vector.

a <- c("a1", "a2", "a3") 
b <- c("b1", "b2", "b3")
c(rbind(a,b))

# [1] "a1" "b1" "a2" "b2" "a3" "b3"

As @Moody_Mudskipper points out, as.vector(rbind(a,b))) is faster

For the case when the lengths are different, I found the following solution from Rolf Turner at this link: http://r.789695.n4.nabble.com/Interleaving-elements-of-two-vectors-td795123.html

riffle <- function (a,b) { 
n <- min(length(a),length(b))
p1 <- as.vector(rbind(a[1:n],b[1:n]))
p2 <- c(a[-(1:n)],b[-(1:n)])
c(p1,p2)
}

riffle(1:3, letters[1:5])

# [1] "1" "a" "2" "b" "3" "c" "d" "e"

Alternate, interweave or interlace two matrices

Here are two alternatives.

First, assuming we have to start with "x" and "y", you can try interleave from the "gdata" package:

library(gdata)
interleave(x, y)
# ranks names
# [1,] "1" "Karl"
# [2,] "" "Cape Town"
# [3,] "2" "Klaus"
# [4,] "" "London"
# [5,] "3" "Mary"
# [6,] "" "Berlin"

Second, assuming we can start with "ranks", "names", and "universities", you can use base R, like this:

cbind(c(t(cbind(ranks, ""))), c(t(cbind(names, universities))))
# [,1] [,2]
# [1,] "1" "Karl"
# [2,] "" "Cape Town"
# [3,] "2" "Klaus"
# [4,] "" "London"
# [5,] "3" "Mary"
# [6,] "" "Berlin"

A better alternative, however, would be to use something like melt (from "reshape2" or "data.table"). This would allow you to add another variable that indicates what type of measurement a value represents.

library(data.table)
melt(data.table(ranks, names, universities), "ranks")
# ranks variable value
# 1: 1 names Karl
# 2: 2 names Klaus
# 3: 3 names Mary
# 4: 1 universities Cape Town
# 5: 2 universities London
# 6: 3 universities Berlin

Or, to match your desired ordering:

library(data.table)
setorder(melt(data.table(ranks, names, universities), "ranks"), ranks)[]
# ranks variable value
# 1: 1 names Karl
# 2: 1 universities Cape Town
# 3: 2 names Klaus
# 4: 2 universities London
# 5: 3 names Mary
# 6: 3 universities Berlin

Combine two vectors alternately

Another possibility:

fun <- function(x, y, n){
c(x, y)[order(c(ceiling(seq_along(x) / n), seq_along(y)))]}

fun(x, y, 1)
# [1] 1 0 2 0 3 0 4 5

fun(x, y, 2)
# [1] 1 2 0 3 4 0 5 0

fun(x, y, 5)
# [1] 1 2 3 4 5 0 0 0

Interweave two columns into one in r

If you wrap the c function around a matrix object (which is what df is above), it will "unwrap them in column-dominant manner (except it needs to be created with rbind so that the interleaving will occur):

c(rbind(col1,col2))
[1] 1 5 2 6 3 7 4 8

If you want it to print at the console as though it were a "column vector" in matrix parlance you could `cbind that result to get a one column matrix object:

cbind( c(rbind(col1,col2)) )
[,1]
[1,] 1
[2,] 5
[3,] 2
[4,] 6
[5,] 3
[6,] 7
[7,] 4
[8,] 8

Combine two equal length vectors alternating

Try

c(rbind(Vector1, Vector2))

Or using Map

unlist(Map(c, Vector1, Vector2))

combine two vectors in correct order

v1 <- c("a","j","d")
v2 <- c("book","tree","river")
c(rbind(v1,v2))
# [1] "a" "book" "j" "tree" "d" "river"

How to merge 2 vectors alternating indexes?

This will work using rbind :

c(rbind(a, b))

For example:

a = c(1,2,3)
b = c(11,12,13)

c(rbind(a,b))

#[1] 1 11 2 12 3 13

R alternate two/four vectors after every two/four values

Here's a wrapper function that will accept any number of vectors and give you desire result (though the vectors are assumed to be of same length)

Myfunc <- function(...){
temp <- cbind(...)
len <- ncol(temp)
suppressWarnings(temp[cbind(seq(nrow(temp)), rep(seq(len), each = len))])
}

Myfunc(A, B)
## [1] 0.11 -0.11 0.20 -0.20 -0.18 0.18

On 4 vectors (Provided by OP in comments)

A <- 1:16  ; B <- 21:36 ; C <- 41:56 ; D <- 61:76
Myfunc(A, B, C, D)
## [1] 1 2 3 4 25 26 27 28 49 50 51 52 73 74 75 76


Related Topics



Leave a reply



Submit