How to randomly split a vector into n vectors of different lengths in R?

R ProgrammingServer Side ProgrammingProgramming

To randomly split a vector into n vectors of different lengths, we can use sample function and set the repl argument to TRUE.

For example, if we have a vector called X of size 100 then we can split it randomly into 10 vectors of different lengths by using the below command −

split(X,sample(10,100,repl=TRUE))

Example 1

To randomly split a vector into n vectors of different lengths, use the following code −

x<-rpois(100,5)
x

The following vector is created −

[1] 5 8 6 0 7 7 3 2 9 7 2 6 7 3 6 5 6 6 3 5 10 2 7 2 6
[26] 4 4 5 2 5 5 5 6 7 8 9 1 5 5 4 4 3 5 2 4 8 3 9 3 10
[51] 2 6 6 9 1 6 3 5 5 9 5 5 7 5 2 6 4 6 3 7 7 8 6 7 2
[76] 3 1 7 7 3 2 6 5 4 4 9 11 5 4 5 4 4 8 6 4 3 4 6 10 4

To split vector x into ten vectors of different lengths, add the following code to the above snippet −

x<-rpois(100,5)
split(x,sample(10,100,repl=TRUE))

Output

If you execute all the above given snippets as a single program, it generates the following output −

$`1`
[1] 7 7 5 9 1 6 9 3 7 7 4 5 8 4

$`2`
[1] 8 6 6 6 4 5 6 9 1 4 5 3 4 2 1 2 4

$`3`
[1] 6 5 6

$`4`
[1] 0 3 6 5 2 5 5 2 5 2 7 3 4 3 10

$`5`
[1] 2 8 10 6 4 4

$`6`
[1] 5 6 7 5 3 2 4 3 9 6 3 7

$`7`
[1] 7 6 5 5 6

$`8`
[1] 9 6 4 8 4 3 7 6 11 6 4

$`9`
[1] 2 5 7 3 5 7 9 4

$`10`
[1] 2 7 2 3 10 5 8 5 5

Example 2

To randomly split a vector into n vectors of different lengths, use the following code −

y<-rnorm(50)
y

The following vector is created −

[1] -0.65895028 -0.56707992 -1.75246557 2.40114713 0.59733276 -0.30155878
[7] 0.88613249 0.22332336 -0.45062795 1.45375437 0.73950598 -0.39335499
[13] 0.15894151 0.31798103 0.33446413 -0.98776965 -1.15817727 0.63471168
[19] -0.31250471 -0.72164519 -1.42849948 -0.17601294 -0.70228905 0.53074750
[25] -1.35596326 1.14057361 0.14610718 0.72637322 -2.12426937 0.12014845
[31] -0.69264761 -0.46707848 0.28632956 -0.42512666 0.67937220 -0.62717506
[37] 0.49865571 -0.97706141 -0.09706441 -0.43790237 0.29795639 1.18133791
[43] 0.02667289 -1.15689193 -0.24730001 -1.70261107 0.05087556 0.82064067
[49] 0.84783593 -0.37130613

To split vector y into ten vectors of different lengths, add the following code to the above snippet −

y<-rnorm(50)
split(y,sample(10,50,repl=TRUE))

Output

If you execute all the above given snippets as a single program, it generates the following output −

$`1`
[1] 1.1405736 0.2863296 0.6793722 -1.7026111 0.8478359

$`2`
[1] 0.3179810 0.1461072 0.4986557

$`3`
[1] 0.22332336 0.73950598 0.15894151 -0.09706441 1.18133791 -1.15689193
[7] -0.24730001

$`4`
[1] 2.4011471 0.5973328 0.5307475 -0.9770614

$`5`
[1] 0.3344641 -0.3125047 -0.6926476

$`6`
[1] -0.56707992 0.88613249 -0.98776965 -0.70228905 -0.43790237 0.05087556
[7] 0.82064067

$`7`
[1] -0.6589503 0.6347117 -1.4284995 0.7263732

$`8`
[1] -1.75246557 1.45375437 -0.39335499 -0.72164519 -2.12426937 0.12014845
[7] -0.62717506 0.29795639 0.02667289

$`9`
[1] -0.450628 -1.158177 -1.355963

$`10`
[1] -0.3015588 -0.1760129 -0.4670785 -0.4251267 -0.3713061
raja
Updated on 11-Nov-2021 05:50:43

Advertisements