0

I'm using R. Let's say I have four numeric x1,x2,x3,x4 and each of them have length about 50 to 100. Then, I want to combine them and I use c(x1,x2,x3,x4). Is there any way that I can randonmly combine them. For example, c(x1,x3,x4,x2),c(x3,x1,x2,x4).

LL WW
  • 1
  • Does this help you ? https://stackoverflow.com/questions/14552528/how-can-i-introduce-values-to-a-vector-in-random-positions-in-r – DataM Dec 09 '21 at 15:09
  • This is not at all clear to me, LL WW. Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. I suggest you create some "random" vectors of much shorter lengths so that you clearly demonstrate some of your desired sampling output. – r2evans Dec 09 '21 at 15:18

1 Answers1

2

Put them in a list, shuffle the order of the list, and then unlist() them into a single vector.

result = list(x1, x2, x3, x4)
result = result[sample(seq_along(result))]
result = unlist(result)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294