- 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 scale some columns in data.table object in R?
To scale some columns in data.table object in R, we can follow the below steps −
First of all, create a data.table object.
hen, subset the columns with single square brackets and use lapply, list and scale function to scale those columns.
Example
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) var1<-rpois(25,5) var2<-rpois(25,2) var3<-rpois(25,5) var4<-rpois(25,3) DT<-data.table(var1,var2,var3,var4) DT
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
var1 var2 var3 var4 1: 5 2 1 2 2: 3 5 4 2 3: 6 2 7 3 4: 10 3 5 3 5: 3 5 3 0 6: 4 1 6 2 7: 4 1 5 3 8: 3 1 6 0 9: 2 2 9 1 10: 2 1 6 6 11: 5 2 5 2 12: 3 1 2 3 13: 4 4 4 1 14: 8 0 5 2 15: 6 1 2 2 16: 4 3 3 3 17: 2 1 7 2 18: 8 2 7 3 19: 9 3 7 0 20: 6 2 6 4 21: 8 2 4 4 22: 4 0 3 2 23: 10 2 4 4 24: 5 3 6 7 25: 5 1 6 1 var1 var2 var3 var4
Scale some columns
Subset the var1 and var2 columns with single square brackets and use lapply, list and scale function to scale those columns as shown below −
DT[,c("var1","var2"):=lapply(list(var1,var2),scale)] DT
Output
var1 var2 var3 var4 1: -0.06546537 0.0000000 1 2 2: -0.88378246 2.2677868 4 2 3: 0.34369318 0.0000000 7 3 4: 1.98032735 0.7559289 5 3 5: -0.88378246 2.2677868 3 0 6: -0.47462391 -0.7559289 6 2 7: -0.47462391 -0.7559289 5 3 8: -0.88378246 -0.7559289 6 0 9: -1.29294100 0.0000000 9 1 10: -1.29294100 -0.7559289 6 6 11: -0.06546537 0.0000000 5 2 12: -0.88378246 -0.7559289 2 3 13: -0.47462391 1.5118579 4 1 14: 1.16201027 -1.5118579 5 2 15: 0.34369318 -0.7559289 2 2 16: -0.47462391 0.7559289 3 3 17: -1.29294100 -0.7559289 7 2 18: 1.16201027 0.0000000 7 3 19: 1.57116881 0.7559289 7 0 20: 0.34369318 0.0000000 6 4 21: 1.16201027 0.0000000 4 4 22: -0.47462391 -1.5118579 3 2 23: 1.98032735 0.0000000 4 4 24: -0.06546537 0.7559289 6 7 25: -0.06546537 -0.7559289 6 1 var1 var2 var3 var4
- Related Articles
- How to standardize columns if some columns are categorical in R data frame?
- How to round each value in columns if some columns are categorical in R data frame?
- How to find the range of columns if some columns are categorical in R data frame?
- How to find the cumulative sum of columns if some columns are categorical in R data frame?
- How to create table of two factor columns in an R data frame?
- How to find the cosine of each value in columns if some columns are categorical in R data frame?
- How to find the log of each value in columns if some columns are categorical in R data frame?
- How to find the exponent of each value in columns if some columns are categorical in R data frame?
- How to find the log10 of each value in columns if some columns are categorical in R data frame?
- How to find the log2 of each value in columns if some columns are categorical in R data frame?
- How to find the rank of each value in columns if some columns are categorical in R data frame?
- How to find the sin of each value in columns if some columns are categorical in R data frame?
- How to round each value to two decimal places in columns if some columns are categorical in R data frame?
- How to find the column variance if some columns are categorical in R data frame?
- How to find the column means if some columns are categorical in R data frame?

Advertisements