[R] Interleaving elements of two vectors?

Jeff Brown dopethatwantscash at yahoo.com
Thu Mar 25 22:10:17 CET 2010


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.



More information about the R-help mailing list