Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two vectors alternately

Tags:

r

From this post, Alternate, interweave or interlace two vectors, a convenient way to combine two vectors alternately was shown:

x <- 1:3 ; y <- 4:6
c(rbind(x, y)) # [1] 1 4 2 5 3 6

However, this method is only applicable to the condition that the two vectors have the same lengths. Now I want to find a general way to do that.

Here is my idea:

  • fun <- function(x, y, n) {...}
  • x and y are two vectors with the same type and their lengths can be different.
  • n means the interval of alternating, and .

The expected output:

x <- 1:5 ; y <- c(0, 0, 0)
fun(x, y, n = 1) # 1 0 2 0 3 0 4 5
fun(x, y, n = 2) # 1 2 0 3 4 0 5 0
fun(x, y, n = 5) # 1 2 3 4 5 0 0 0

Thanks for help.

like image 347
Darren Tsai Avatar asked Dec 16 '18 12:12

Darren Tsai


3 Answers

First split x into groups of n and stack it into a long data.frame. Similarly stack y and then rbind those data frames together, order them by ind (the group number) and take the values column.

fun <- function(x, y, n) {
  sx <- stack(split(x, c(gl(length(x), n, length(x)))))
  sy <- stack(as.list(setNames(y, seq_along(y))))
  r <- rbind(sx, sy)
  r[order(r$ind), "values"]
}

x <- 1:5 ; y <- c(0, 0, 0)

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
like image 146
G. Grothendieck Avatar answered Oct 24 '22 10:10

G. Grothendieck


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
like image 43
Henrik Avatar answered Oct 24 '22 09:10

Henrik


a solution using Reduce :

n <- 2
res <- Reduce(function(x,i) append(x,y[i],i),n*(length(y):1), x)
res[is.na(res)] <- 0
res
# [1] 1 2 0 3 4 0 5 0
like image 24
Moody_Mudskipper Avatar answered Oct 24 '22 09:10

Moody_Mudskipper