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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With