- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to randomize column values of a data.table object for all columns in R?
- How to create a table of frequency for range of values in an R data frame column?
- How to set NA values to TRUE for a Boolean column in an R data frame?
- How to create an ID column for the combination of values in multiple columns in R data frame?
- How to convert a data frame into table for two factor columns and one numeric column in R?
- How to repeat column values of a data.table object in R by number of values in another column?
- How to create a new column with means of row values for each or some of the columns in an R data frame?
- Set DEFAULT values for columns while creating a table in MySQL
- How to repeat the values stored in a data column of MySQL table?
- Set values in categorical column to numeric values in R data frame.
- How to randomize rows of a matrix in R?
- How to find the mean of a column summarized by other column names and common values in those columns in R data frame?
- How to multiply vector values in sequence with columns of a data.table object in R?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How can I return the values of columns from MySQL table as a set of values?

Advertisements