# How to randomize column values of a data.table object for a set of columns in R?

To randomize column values of a data.table object for a set of columns in R, we can follow the below steps −

• First of all, create a data.table object.
• Then, use sample function for randomizationwith lapply while selecting the columns with SDcols.

## Create the data frame

Let's create a data frame as shown below −

### Example

library(data.table)
ID<-1:20
x<-rpois(20,5)
y<-rpois(20,10)
z<-rpois(20,8)
DT<-data.table(ID,x,y,z)
DT

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

### Output

  ID x y z
1: 1 6 9 8
2: 2 4 11 7
3: 3 2 11 10
4: 4 2 9 14
5: 5 9 8 7
6: 6 1 12 9
7: 7 8 9 8
8: 8 5 6 5
9: 9 6 14 4
10: 10 5 7 3
11: 11 3 11 13
12: 12 8 13 7
13: 13 5 14 10
14: 14 5 12 6
15: 15 5 4 10
16: 16 7 10 7
17: 17 7 12 3
18: 18 5 6 9
19: 19 4 14 8
20: 20 3 9 6

## Randomize column values for selected columns in the data.table object

Using lapply and sample function to randomize the column values of column 2, 3, and 4 in DT −

### Example

library(data.table)
ID<-1:20
x<-rpois(20,5)
y<-rpois(20,10)
z<-rpois(20,8)
DT<-data.table(ID,x,y,z)
DT[,(2:4):=lapply(.SD,sample),.SDcols=2:4]
DT

### Output

    ID x  y  z
1:  1  4  11 7
2:  2  2  9  7
3:  3  5  6  14
4:  4  3  9  3
5:  5  8  11 6
6:  6  7  14 10
7:  7  1  7  13
8:  8  5  9 10
9:  9  5  14 4
10: 10 2  12 7
11: 11 5  9 8
12: 12 8  12 7
13: 13 3  8 10
14: 14 5  6 8
15: 15 9  4 5
16: 16 6  14 9
17: 17 5  10 6
18: 18 7  12 3
19: 19 6  11 9
20: 20 4  13 8

Updated on: 07-Aug-2021

120 Views