# [R] Interleaving elements of two vectors?

Henrique Dallazuanna wwwhsd at gmail.com
Thu Mar 25 23:32:25 CET 2010

```Try this also:

riffle3 <- function(a, b) {
mNrow <- nrow(cbind(a, b))
m <- as.data.frame(cbind(a[1:mNrow], b[1:mNrow]))
as.numeric(na.exclude(unlist(lapply(split(m, 1:mNrow), as.numeric))))
}

On Thu, Mar 25, 2010 at 6:10 PM, Jeff Brown <dopethatwantscash at yahoo.com> wrote:
>
> I just had to solve this problem for myself, after not having luck with the
> code posted above.  I'm posting in case others need a completely general
> function.
>
> riffle <- function (a,b) {
>        # Interleave a & b, starting with a, without repeating.
>        x <- NULL;              count = 1;
>        for (i in 1:max(length(a), length(b))) {
>                if (i <= length(a)) {
>                        x[count] <- a[i];
>                        count = count+1;
>                };
>                if (i <= length(b)) {
>                        x[count] <- b[i];
>                        count = count+1;
>                }
>        };
>        x
> };
> riffle( 1:10, 50:55 )
>
> --
> View this message in context: http://n4.nabble.com/Interleaving-elements-of-two-vectors-tp795123p1691317.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

```