1

I have two vectors (they can be different length, can contain NA's)

chars <- letters[1:3]
nums <- 1:5

I want to shuffle them so that I have a single vector: chars[1], nums[1], chars[2], nums[2], ..., chars[n], nums[n]. My current approach is the following

as.vector(vapply(1:5, function(i) c(chars[i], nums[i]), character(2)))

Is there a better way to do this? I don't like about my solution the transformation vector -> matrix -> vector. Using a for-loop is an obvious alternative but I am looking for better ones if possible.

Tim
  • 7,075
  • 6
  • 29
  • 58

0 Answers0