Home > Software engineering >  scale the columns of a dataset excluding non-progressive columns in r
scale the columns of a dataset excluding non-progressive columns in r

Time:05-25

How can I scale the columns of a dataset excluding non-progressive columns in r? For example, I have to exclude column 1, column 5 and column 7.

CodePudding user response:

Do you want this one?

ex_dataset<-data.frame(A=c(1,2,3),
                       B=c(1,2,3),
                       C=c(1,2,3),
                       D=c(1,2,3),
                       E=c(1,2,3),
                       F=c(1,2,3),
                       G=c(1,2,3))
ex_dataset
#  A B C D E F G
#1 1 1 1 1 1 1 1
#2 2 2 2 2 2 2 2
#3 3 3 3 3 3 3 3

ex_dataset[,-c(1,5,7)]
#  B C D F
#1 1 1 1 1
#2 2 2 2 2
#3 3 3 3 3

CodePudding user response:

Maybe you want this:

df <- data.frame(A = sample(1:100, 5, replace=TRUE),
                 B = sample(1:100, 5, replace=TRUE),
                 C = sample(1:100, 5, replace=TRUE),
                 D = sample(1:100, 5, replace=TRUE),
                 E = sample(1:100, 5, replace=TRUE),
                 `F` = sample(1:100, 5, replace=TRUE),
                 G = sample(1:100, 5, replace=TRUE))

data:

   A  B  C  D  E  F  G
1 28 76 69  7 54 53 65
2 71 86 11 69 15  7 84
3 48 10 63 84 34 37 64
4  5 61 18 17 63 31 87
5 57 53 75 44 82 69 79

You can scale certain columns like this:

df[c(1, 5, 7)] <- scale(df[c(1, 5, 7)])

Output:

       A  B  C  D      E  F      G
1 -0.534 76 69  7  0.170 53 -1.008
2  1.131 86 11 69 -1.334  7  0.766
3  0.240 10 63 84 -0.602 37 -1.102
4 -1.425 61 18 17  0.517 31  1.046
5  0.589 53 75 44  1.250 69  0.299
  • Related