- 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 the R data frame by excluding a particular column?
To scale the R data frame by excluding a particular column, we can follow the below steps −
First of all, create a data frame.
Then, subset the data frame with single square brackets and scale function.
Create the data frame
Let’s create a data frame as shown below −
Group<-sample(LETTERS[1:5],25,replace=TRUE) x<-sample(1:100,25) y<-sample(1:100,25) z<-sample(1:100,25) df<-data.frame(Group,x,y,z) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Output
Group x y z 1 B 45 90 77 2 C 16 23 12 3 A 89 5 37 4 B 100 43 95 5 A 22 60 31 6 A 33 3 32 7 A 39 56 40 8 C 72 98 15 9 C 10 57 91 10 E 11 95 87 11 D 74 79 59 12 A 2 33 4 13 D 59 4 7 14 B 43 83 64 15 B 29 55 24 16 B 20 37 81 17 C 76 38 73 18 D 97 58 65 19 C 25 68 76 20 E 67 32 79 21 A 95 19 88 22 D 54 1 75 23 D 83 96 85 24 A 81 94 52 25 B 69 77 82
Scaling df by excluding a column
Using scale function to subset all columns except first column of df −
Group<-sample(LETTERS[1:5],25,replace=TRUE) x<-sample(1:100,25) y<-sample(1:100,25) z<-sample(1:100,25) df<-data.frame(Group,x,y,z) df[,-1]<-scale(df[,-1]) df
Output
Group x y z 1 B -0.24323729 1.18343070 0.67465163 2 C -1.19133961 -0.91196721 -1.54459716 3 A 1.19526280 -1.47490993 -0.69103993 4 B 1.55488782 -0.28647530 1.28921283 5 A -0.99518051 0.24519283 -0.89589366 6 A -0.63555549 -1.53745912 -0.86175138 7 A -0.43939639 0.12009445 -0.58861306 8 C 0.63947867 1.43362746 -1.44217029 9 C -1.38749872 0.15136904 1.15264368 10 E -1.35480553 1.33980368 1.01607452 11 D 0.70486504 0.83941015 0.06009043 12 A -1.64904418 -0.59922125 -1.81773547 13 D 0.21446728 -1.50618453 -1.71530860 14 B -0.30862365 0.96450853 0.23080187 15 B -0.76632823 0.08881985 -1.13488969 16 B -1.06056688 -0.47412287 0.81122079 17 C 0.77025141 -0.44284827 0.53808248 18 D 1.45680826 0.18264364 0.26494416 19 C -0.89710096 0.49538960 0.64050934 20 E 0.47601275 -0.63049585 0.74293621 21 A 1.39142190 -1.03706559 1.05021681 22 D 0.05100137 -1.60000831 0.60636705 23 D 0.99910369 1.37107827 0.94778994 24 A 0.93371733 1.30852908 -0.17890559 25 B 0.54139912 0.77686096 0.84536308
- Related Articles
- How to find the row sums by excluding a column in R data frame?
- How to filter rows by excluding a particular value in columns of the R data frame?
- How to subset a data frame by excluding a column using dplyr in R?
- Create a sample of data frame column by excluding NAs in R
- How to subset a data frame by excluding a specific text value in an R data frame?
- How to subset a data frame by excluding the column names that are stored in a vector in R?
- How to find the average of a particular column in R data frame?
- How to split a data frame by column in R?
- How to check whether a particular word exists in an R data frame column?
- How to calculate row means by excluding NA values in an R data frame?
- How to find the smallest number in an R data frame column excluding values zero or less?
- How to extract a particular value based on index from an R data frame column?
- Extract a particular level from factor column in an R data frame.
- How to find the column mean by excluding NA’s and if all values are NA then output NA in R data frame?
- How to create a subset of an R data frame having complete cases of a particular column?

Advertisements