# 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 −

Live Demo

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 −

Live Demo

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