# How to randomize an already created vector in R?

Some vectors are randomly created and some are not randomly created in R but we can do randomization for both of these types of vectors. Randomization ensures unbiasedness therefore it is necessary especially when the vector is created with an objective that tends to change the result of the analysis. The randomization in R can be simply done with the help of sample function.

Randomization of vectors that are not randomly created −

> x1<-1:30
> x1
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30
> sample(x1)
[1] 18 24 20 2 26 15 14 9 13 1 16 27 30 29 6 22 3 12 5 10 19 8 17 21 7
[26] 25 11 23 28 4
> x2<-letters[1:26]
> x2
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
> sample(x2)
[1] "s" "f" "z" "w" "k" "c" "e" "m" "b" "t" "x" "d" "v" "y" "r" "g" "i" "o" "p"
[20] "h" "u" "n" "j" "a" "l" "q"
> x3<-rep(c(1,2,3,4,5),each=10)
> x3
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4
[39] 4 4 5 5 5 5 5 5 5 5 5 5
> sample(x3)
[1] 5 4 2 1 1 4 3 3 2 1 3 5 4 5 5 1 2 1 3 5 2 1 3 4 5 3 1 2 4 3 4 5 2 4 3 5 2 2
[39] 5 4 1 2 5 1 3 1 3 4 2 4

Randomization of vectors that are randomly created −

> x4<-rnorm(20,0.5)
> x4
[1] 0.46076000 1.18973936 0.52800216 -0.24327321 0.68879230 -1.30495863
[7] 1.96555486 0.65325334 2.67261167 0.97550953 -0.20994643 1.11072635
[13] -0.43409763 -0.75363340 0.79144624 0.05670813 0.50110535 0.57434132
[19] -0.08952095 -0.06866873
> sample(x4)
[1] -0.75363340 0.50110535 0.52800216 0.57434132 1.96555486 -0.06866873
[7] -0.08952095 0.79144624 1.11072635 0.46076000 2.67261167 1.18973936
[13] 0.65325334 -1.30495863 -0.20994643 0.97550953 -0.43409763 -0.24327321
[19] 0.05670813 0.68879230
> x5<-rpois(30,2)
> x5
[1] 5 3 1 2 5 5 1 1 1 1 2 4 2 1 0 2 3 1 0 1 2 1 3 3 2 2 2 1 2 4
> sample(x5)
[1] 3 5 1 3 1 5 3 1 5 2 4 1 2 2 2 2 1 2 1 1 1 2 0 3 1 4 2 2 1 0
> x6<-runif(30,2,5)
> x6
[1] 3.119190 2.143877 2.415885 2.964476 2.464495 2.396685 2.663918 2.679142
[9] 2.394250 4.944690 2.981041 3.520818 4.044328 2.297507 2.356708 2.151319
[17] 4.787762 4.021137 2.284574 3.477788 3.384656 3.125650 4.973298 2.529052
[25] 4.440306 2.205340 3.201349 2.423433 2.579930 4.524055
> sample(x6)
[1] 4.044328 2.394250 4.440306 2.663918 2.423433 2.297507 2.464495 3.201349
[9] 3.477788 3.125650 4.944690 2.679142 3.119190 2.205340 2.356708 3.520818
[17] 4.524055 2.151319 3.384656 2.143877 4.787762 4.021137 2.579930 2.964476
[25] 4.973298 2.529052 2.284574 2.981041 2.396685 2.415885
> x7<-rep(c("Apple","Guava","Banana","Kiwi","Mango","Orange"),times=10)
> x7
[1] "Apple" "Guava" "Banana" "Kiwi" "Mango" "Orange" "Apple" "Guava"
[9] "Banana" "Kiwi" "Mango" "Orange" "Apple" "Guava" "Banana" "Kiwi"
[17] "Mango" "Orange" "Apple" "Guava" "Banana" "Kiwi" "Mango" "Orange"
[25] "Apple" "Guava" "Banana" "Kiwi" "Mango" "Orange" "Apple" "Guava"
[33] "Banana" "Kiwi" "Mango" "Orange" "Apple" "Guava" "Banana" "Kiwi"
[41] "Mango" "Orange" "Apple" "Guava" "Banana" "Kiwi" "Mango" "Orange"
[49] "Apple" "Guava" "Banana" "Kiwi" "Mango" "Orange" "Apple" "Guava"
[57] "Banana" "Kiwi" "Mango" "Orange"
> sample(x7)
[1] "Apple" "Guava" "Banana" "Guava" "Mango" "Mango" "Guava" "Orange"
[9] "Banana" "Guava" "Guava" "Orange" "Banana" "Apple" "Banana" "Apple"
[17] "Banana" "Guava" "Kiwi" "Orange" "Mango" "Mango" "Guava" "Banana"
[25] "Kiwi" "Kiwi" "Mango" "Mango" "Banana" "Apple" "Orange" "Orange"
[33] "Apple" "Apple" "Guava" "Apple" "Kiwi" "Apple" "Kiwi" "Kiwi"
[41] "Kiwi" "Orange" "Orange" "Banana" "Guava" "Apple" "Orange" "Mango"
[49] "Kiwi" "Mango" "Mango" "Orange" "Mango" "Orange" "Kiwi" "Guava"
[57] "Banana" "Kiwi" "Apple" "Banana"

Updated on: 11-Aug-2020

504 Views